Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

GerryChain

Code Coverage Documentation Status PyPI Package

GerryChain is a Python library for building ensembles of districting plans using Markov chain Monte Carlo. It is developed and maintained by the Metric Geometry and Gerrymandering Group and our network of volunteers. It is distributed under the 3-Clause BSD License.

The basic workflow is to start with the geometry of an initial plan and generate a large collection of sample plans for comparison. Usually, we will constrain these sampled plans in such a way that they perform at least as well as the initial plan according to traditional districting principles, such as population balance or compactness. Comparing the initial plan to the ensemble provides quantitative tools for measuring whether or not it is an outlier among the sampled plans.

Getting started

See our Getting started guide for the basics of using GerryChain.

We also highly recommend the resources prepared by Daryl R. DeFord of MGGG for the 2019 MIT IAP course Computational Approaches for Political Redistricting.

Useful links

Installation

Supported Python Versions

The current version of GerryChain requires Python 3.11 or newer.

If you do not have one of these versions installed on you machine, we recommend that you go to the Python website and download the installer for one of these versions.1

A Note for Windows Users

If you are using Windows and are new to Python, we recommend that you still install Python using the installation package available on the Python website. There are several versions of Python available on the Windows Store, but they can be... finicky, and experience seems to suggest that downloadable available on the Python website produce better results.

In addition, we recommend that you install the Windows Terminal from the Microsoft Store. It is still possible to use PowerShell or the Command Prompt, but Windows Terminal tends to be more beginner friendly and allows for a greater range of utility than the natively installed terminal options (for example, it allows for you to install the more recent version of PowerShell, PowerShell 7, and for the use of the Linux Subsystem for Windows).

Setting Up a Virtual Environment

Once Python is installed on your system, you will want to open the terminal and navigate to the working directory of your project. Here are some brief instructions for doing so on different systems:

  • MacOS: To open the terminal, you will likely want to use the Spotlight Search (the magnifying glass in the top right corner of your screen) to find the "Terminal" application (you can also access Spotlight Search by pressing "Command (⌘) + Space"). Once you have the terminal open, type cd followed by the path to your working directory. For example, if you are working on a project called my_project in your Documents folder, you may access by typing the command

    cd ~/Documents/my_project
    

    into the terminal (here the ~ is a shortcut for your home directory). If you do not know what your working directory is, you can find it by navigating to the desired folder in your file explorer, and clicking on "Get Info". The path will be labeled "Where" and from there you can copy the path to your clipboard and paste it in the terminal.

  • Linux: Most Linux distributions have the keyboard shortcut Ctrl + Alt + T set to open the terminal. From there you may navigate to your working directory by typing cd followed by the path to your working directory. For example, if you are working on a project called my_project in your Documents folder, you may access this via the command

    cd ~/Documents/my_project
    

    (here the ~ is a shortcut for your home directory). If you do not know what your working directory is, you can find it by navigating to the desired folder in your file explorer, and clicking on "Properties". The path will be labeled "Location" and from there you can copy the path to your clipboard and paste it in the terminal (to paste in the terminal in Linux, you will need to use the keyboard shortcut Ctrl + Shift + V instead of Ctrl + V).

  • Windows: Open the Windows Terminal and type cd followed by the path to your working directory. For example, if you are working on a project called my_project in your Documents folder, you may access this by typing the command

    cd ~\Documents\my_project
    

    into the terminal (here the ~ is a shortcut for your home directory). If you do not know what your working directory is, you can find it by navigating to the desired folder in your file explorer, and clicking on "Properties". The path will be labeled "Location" and from there you can copy the path to your clipboard and paste it in the terminal.

Once you have navigated to your working directory, you will want to set up a virtual environment. This is a way of isolating the Python packages you install for this project from the packages you have installed globally on your system. This is useful because it allows you to install different versions of packages for different projects without worrying about compatibility issues. To set up a virtual environment, type the following command into the terminal:

python -m venv .venv

This will create a virtual environment in your working directory which you can see if you list all the files in your working directory via the command ls -a (dir on Windows). Now we need to activate the virtual environment. To do this, type the following command into the terminal:

  • Windows: .venv\Scripts\activate
  • MacOS/Linux: source .venv/bin/activate

You should now see (.venv) at the beginning of your terminal prompt now. This indicates that you are in the virtual environment, and are now ready to install GerryChain.

To install GerryChain from PyPI, run pip install gerrychain from the command line.

GerryChain's GIS functions, such as computing adjacencies or reading in shapefiles, work out of the box: geopandas and shapely are installed alongside GerryChain, so there is no separate [geo] extra to install.

Note If you plan on following through the tutorials present within the remainder of this documentation, you will also need to install matplotlib from PyPI. This can also be accomplished with a simple invocation of pip install matplotlib from the command line.

Making a Run Reproducible

Each MarkovChain owns an independent random number generator, so making a run reproducible takes one line: pass an integer seed or random.Random instance with MarkovChain(..., rng=2024). Random initial assignments and standalone tree operations accept the same rng keyword.

Note that random.seed from the standard library does not affect GerryChain, and that chain trajectories do not depend on the PYTHONHASHSEED environment variable (older versions of GerryChain required pinning it, but that is no longer necessary).

A note on Jupyter

If you are using a Jupyter notebook, install ipykernel in your virtual environment along with either jupyter notebook or jupyterlab. Then run jupyter notebook or jupyter lab from your working directory while the virtual environment is active. This opens Jupyter in your default browser.

  1. Of course, if you are using a Linux system, you will either need to use your system's package manager or install from source. You may also find luck installing Python directly from the package manager if you find installing from source to be troublesome.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gerrychain-1.0.0rc2.tar.gz (197.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gerrychain-1.0.0rc2-py3-none-any.whl (174.6 kB view details)

Uploaded Python 3

File details

Details for the file gerrychain-1.0.0rc2.tar.gz.

File metadata

  • Download URL: gerrychain-1.0.0rc2.tar.gz
  • Upload date:
  • Size: 197.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gerrychain-1.0.0rc2.tar.gz
Algorithm Hash digest
SHA256 53a6b3fd24c9b1fc2943192dda83506ef267dd7b987e78d2e68bd5d2294ac0b5
MD5 c2fe495740357d9d7519bfc518add2ad
BLAKE2b-256 e8195edff7faae7a06a58e7b387fc8ebfc099e24129b1dbde3af2b9f29967789

See more details on using hashes here.

File details

Details for the file gerrychain-1.0.0rc2-py3-none-any.whl.

File metadata

  • Download URL: gerrychain-1.0.0rc2-py3-none-any.whl
  • Upload date:
  • Size: 174.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.1 {"installer":{"name":"uv","version":"0.12.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gerrychain-1.0.0rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 225ec15504f455588e9fa8f76029adef4e2b1593b15924d9bb12d9c8d6d1845e
MD5 98b608f3cef20fc1fb192ed75139527c
BLAKE2b-256 eca5dba56831157ff74cdb760ebc3c01c77fecf100de0e5f9f6372e1c314fe4d

See more details on using hashes here.

Release history Release notifications | RSS feed

1.0.0

2 files

This release

1.0.0rc2 This release

2 files

0.3.2

2 files

0.3.1

2 files

0.3.0

1 file

0.2.22

2 files

0.2.20

3 files

0.2.19

3 files

0.2.18

3 files

0.2.17

2 files

0.2.15

2 files

0.2.14

2 files

0.2.12

2 files

0.2.11

2 files

0.2.10

2 files

0.2.9

2 files

0.2.8

3 files

0.2.7

2 files

0.2.6

2 files

0.2.5

2 files

0.2.4

2 files

0.2.3

2 files

0.2.2

2 files

0.2.1

2 files

0.2.0

3 files

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