Skip to main content

Use Markov chain Monte Carlo to analyze districting plans and gerrymanders

Project description

Build Status 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.

Installation

Supported Python Versions

The most recent version of GerryChain (as of April 2024) supports

  • Python 3.9

  • Python 3.10

  • Python 3.11

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.

If you plan on using GerryChain’s GIS functions, such as computing adjacencies or reading in shapefiles, then run pip install gerrychain[geo] from the command line.

This approach sometimes fails due to compatibility issues between our different Python GIS dependencies, like geopandas, pyproj, fiona, and shapely. If you run into this issue, try installing the dependencies using the geo_settings.txt file. To do this, run pip install -r geo_settings.txt from the command line.

Making an Environment Reproducible

If you are working on a project wherein you would like to ensure particular runs are reproducible, it is necessary to invoke

  • MacOS/Linux: export PYTHONHASHSEED=0

  • Windows:

    • PowerShell $env:PYTHONHASHSEED=0

    • Command Prompt set PYTHONHASHSEED=0

before running your code. This will ensure that the hash seed is deterministic which is important for the replication of spanning trees across your runs. If you would prefer to not have to do this every time, then you need to modify the activation script for the virtual environment. Again, this is different depending on your operating system:

  • MacOS/Linux: Open the file .venv/bin/activate located in your working directory using your favorite text editor and add the line export PYTHONHASHSEED=0 after the export PATH command. So you should see something like:

    _OLD_VIRTUAL_PATH="$PATH"
    PATH="$VIRTUAL_ENV/Scripts:$PATH"
    export PATH
    
    export PYTHONHASHSEED=0

    Then, verify that the hash seed is set to 0 in your Python environment by running python from the command line and typing import os; print(os.environ['PYTHONHASHSEED']).

  • Windows: To be safe, you will need to modify 3 files within your virtual environment:

    • .venv\Scripts\activate: Add the line export PYTHONHASHSEED=0 after the export PATH command. So you should see something like:

      _OLD_VIRTUAL_PATH="$PATH"
      PATH="$VIRTUAL_ENV/Scripts:$PATH"
      export PATH
      
      export PYTHONHASHSEED=0
    • .venv\Scripts\activate.bat: Add the line set PYTHONHASHSEED=0 to the end of the file. So you should see something like:

      if defined _OLD_VIRTUAL_PATH set PATH=%_OLD_VIRTUAL_PATH%
      if not defined _OLD_VIRTUAL_PATH set _OLD_VIRTUAL_PATH=%PATH%
      
      set PATH=%VIRTUAL_ENV%\Scripts;%PATH%
      rem set VIRTUAL_ENV_PROMPT=(.venv)
      set PYTHONHASHSEED=0
    • .venv\Scripts\Activate.ps1: Add the line $env:PYTHONHASHSEED=0 to the end of the before the signature block. So you should see something like:

      # Add the venv to the PATH
      Copy-Item -Path Env:PATH -Destination Env:_OLD_VIRTUAL_PATH
      $Env:PATH = "$VenvExecDir$([System.IO.Path]::PathSeparator)$Env:PATH"
      
      $env:PYTHONHASHSEED=0
      
      # SIG # Begin signature block

After you have made these changes, verify that the hash seed is set to 0 in your Python environment by running python from the command line and typing import os; print(os.environ['PYTHONHASHSEED']) in the Python prompt.

Project details


Download files

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

Source Distribution

gerrychain-0.3.2.tar.gz (95.6 kB view details)

Uploaded Source

Built Distribution

gerrychain-0.3.2-py2.py3-none-any.whl (87.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file gerrychain-0.3.2.tar.gz.

File metadata

  • Download URL: gerrychain-0.3.2.tar.gz
  • Upload date:
  • Size: 95.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for gerrychain-0.3.2.tar.gz
Algorithm Hash digest
SHA256 321d1ebd34408aa4cae457220365ce76a7e12f56efe43a81d34cede9bb4377df
MD5 ddd5e4022f115a71435d4840f35ef0eb
BLAKE2b-256 94e0a446e0f4880639fb7c55bc7882b6364bd175d595f9bb0d7b4eb205e198df

See more details on using hashes here.

File details

Details for the file gerrychain-0.3.2-py2.py3-none-any.whl.

File metadata

  • Download URL: gerrychain-0.3.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 87.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.8

File hashes

Hashes for gerrychain-0.3.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1241d16f4a7374d679227ea5a9eacae9db9386e1598ac02daeded7d4d007035e
MD5 23607b98ac752a45f9a5d53f15fced71
BLAKE2b-256 a20190cdaa1bfb1536e1aa773671baccafe3bfde648960c584416a9e26bea9f8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page