{"id":24513616,"url":"https://github.com/andrewtavis/poli-sci-kit","last_synced_at":"2025-04-14T08:51:01.235Z","repository":{"id":52381646,"uuid":"263402494","full_name":"andrewtavis/poli-sci-kit","owner":"andrewtavis","description":"Political elections, appointment, analysis and visualization in Python","archived":false,"fork":false,"pushed_at":"2024-02-04T10:55:35.000Z","size":4640,"stargazers_count":30,"open_issues_count":3,"forks_count":6,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-22T17:41:55.261Z","etag":null,"topics":["analysis","appointment","democracy","disproportionality","elections","open-source","political-appointment-analysis","political-science","politics","python","python3","statistics","visualization","voting"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/andrewtavis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.txt","code_of_conduct":".github/CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-05-12T17:16:50.000Z","updated_at":"2025-02-22T01:23:59.000Z","dependencies_parsed_at":"2023-11-24T20:24:58.528Z","dependency_job_id":"1bfc663d-26b2-464c-b6fa-33c7e72c76a0","html_url":"https://github.com/andrewtavis/poli-sci-kit","commit_stats":{"total_commits":245,"total_committers":3,"mean_commits":81.66666666666667,"dds":0.008163265306122436,"last_synced_commit":"5c480c8420e43bb6e6cc32ca55f46c5b5fb75c39"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewtavis%2Fpoli-sci-kit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewtavis%2Fpoli-sci-kit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewtavis%2Fpoli-sci-kit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/andrewtavis%2Fpoli-sci-kit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/andrewtavis","download_url":"https://codeload.github.com/andrewtavis/poli-sci-kit/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248852079,"owners_count":21171837,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["analysis","appointment","democracy","disproportionality","elections","open-source","political-appointment-analysis","political-science","politics","python","python3","statistics","visualization","voting"],"created_at":"2025-01-22T00:55:43.618Z","updated_at":"2025-04-14T08:51:01.214Z","avatar_url":"https://github.com/andrewtavis.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003ca href=\"https://github.com/andrewtavis/poli-sci-kit\"\u003e\u003cimg src=\"https://raw.githubusercontent.com/andrewtavis/poli-sci-kit/main/.github/resources/logo/poli-sci-kit_logo_transparent.png\" width=463 height=251\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n\u003col\u003e\u003c/ol\u003e\n\n[![rtd](https://img.shields.io/readthedocs/poli-sci-kit.svg?logo=read-the-docs)](http://poli-sci-kit.readthedocs.io/en/latest/)\n[![ci](https://img.shields.io/github/actions/workflow/status/andrewtavis/poli-sci-kit/.github/workflows/ci.yml?branch=main?logo=github)](https://github.com/andrewtavis/poli-sci-kit/actions?query=workflow%3ACI)\n[![codecov](https://codecov.io/gh/andrewtavis/poli-sci-kit/branch/main/graphs/badge.svg)](https://codecov.io/gh/andrewtavis/poli-sci-kit)\n[![pyversions](https://img.shields.io/pypi/pyversions/poli-sci-kit.svg?logo=python\u0026logoColor=FFD43B\u0026color=306998)](https://pypi.org/project/poli-sci-kit/)\n[![pypi](https://img.shields.io/pypi/v/poli-sci-kit.svg?color=4B8BBE)](https://pypi.org/project/poli-sci-kit/)\n[![pypistatus](https://img.shields.io/pypi/status/poli-sci-kit.svg)](https://pypi.org/project/poli-sci-kit/)\n[![license](https://img.shields.io/github/license/andrewtavis/poli-sci-kit.svg)](https://github.com/andrewtavis/poli-sci-kit/blob/main/LICENSE.txt)\n[![coc](https://img.shields.io/badge/coc-Contributor%20Covenant-ff69b4.svg)](https://github.com/andrewtavis/poli-sci-kit/blob/main/.github/CODE_OF_CONDUCT.md)\n[![codestyle](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n[![colab](https://img.shields.io/badge/%20-Open%20in%20Colab-097ABB.svg?logo=google-colab\u0026color=097ABB\u0026labelColor=525252)](https://colab.research.google.com/github/andrewtavis/poli-sci-kit)\n\n## Political elections, appointment, analysis and visualization in Python\n\n**poli-sci-kit** is a Python package for political science appointment and election analysis. The goal is to provide a comprehensive tool for all methods needed to analyze and simulate election results. See the [documentation](https://poli-sci-kit.readthedocs.io/en/latest/) for a full outline of the package including algorithms and visualization techniques.\n\n\u003ca id=\"contents\"\u003e\u003c/a\u003e\n\n# **Contents**\n\n- [Installation](#installation)\n- [Appointment](#appointment)\n- [Plotting](#plotting)\n  - [Parliament Plots](#parliament-plots)\n  - [Disproportionality Bar Plot](#disproportionality-bar-plot)\n- [Examples](#examples)\n- [To-Do](#to-do)\n\n\u003ca id=\"installation\"\u003e\u003c/a\u003e\n\n# Installation [`⇧`](#contents)\n\npoli-sci-kit can be downloaded from PyPI via pip or sourced directly from this repository:\n\n```bash\npip install poli-sci-kit\n```\n\n```bash\ngit clone https://github.com/andrewtavis/poli-sci-kit.git\ncd poli-sci-kit\npython setup.py install\n```\n\n```python\nimport poli_sci_kit\n```\n\n\u003ca id=\"appointment\"\u003e\u003c/a\u003e\n\n# Appointment [`⇧`](#contents)\n\n[appointment.methods](https://github.com/andrewtavis/poli-sci-kit/blob/main/src/poli_sci_kit/appointment/methods.py) includes functions to allocate parliamentary seats based on population or vote shares. Included methods are:\n\n#### Largest Remainder: Hare, Droop, Hagenbach–Bischoff (incl Hamilton, Vinton, Hare–Niemeyer)\n\n#### Highest Averages: Jefferson, Webster, Huntington-Hill\n\nArguments to allow allocation thresholds, minimum allocations per group, tie break conditions, and other election features are also provided. Along with deriving results for visualization and reporting, these functions allow the user to analyze outcomes given systematic or situational changes. The [appointment.metrics](https://github.com/andrewtavis/poli-sci-kit/blob/main/src/poli_sci_kit/appointment/metrics.py) module further provides diagnostics to analyze the results of elections, apportionments, and other political science scenarios.\n\nA basic example of political appointment using poli-sci-kit is:\n\n```python\nfrom poli_sci_kit import appointment\n\nvote_counts = [2700, 900, 3300, 1300, 2150, 500]\nseats_to_allocate = 50\n\n# Huntington-Hill is the method used to allocate House of Representatives seats to US states\nha_allocations = appointment.methods.highest_averages(\n    averaging_style=\"Huntington-Hill\",\n    shares=vote_counts,\n    total_alloc=seats_to_allocate,\n    alloc_threshold=None,\n    min_alloc=1,\n    tie_break=\"majority\",\n    majority_bonus=False,\n    modifier=None,\n)\n\nha_allocations\n# [26, 9, 37, 12, 23, 5]\n```\n\nWe can then compute various metrics to derive disproportionality:\n\n```python\n# The Gallagher method is a measure of absolute difference similar to summing square residuals\ndisproportionality = appointment.metrics.dispr_index(\n    shares=vote_counts,\n    allocations=ha_allocations,\n    metric_type='Gallagher'\n)\n\ndisproportionality\n# 0.01002\n```\n\nWe can also check that the allocations pass the [quota condition](https://en.wikipedia.org/wiki/Quota_rule):\n\n```python\npasses_qc = appointment.checks.quota_condition(\n    shares=vote_counts,\n    seats=ha_allocations\n)\n\npasses_qc\n# True\n```\n\nAllocation consistency can further be checked using dataframes of shares and seats given electoral settings. See [appointment.checks](https://github.com/andrewtavis/poli-sci-kit/blob/main/src/poli_sci_kit/appointment/checks.py) and [the documentation](https://poli-sci-kit.readthedocs.io/en/latest/) for explanations of method checks.\n\n\u003ca id=\"plotting\"\u003e\u003c/a\u003e\n\n# Plotting [`⇧`](#contents)\n\npoli-sci-kit provides Python only implementations of common electoral plots.\n\nVisualizing the above results:\n\n```python\nimport matplotlib.pyplot as plt\nimport poli_sci_kit\n\n# German political parties\nparties = ['CDU/CSU', 'FDP', 'Greens', 'Die Linke', 'SPD', 'AfD']\nparty_colors = ['#000000', '#ffed00', '#64a12d', '#be3075', '#eb001f', '#009ee0']\n```\n\n\u003ca id=\"parliament-plots\"\u003e\u003c/a\u003e\n\n### Parliament Plots [`⇧`](#contents)\n\npoli_sci_kit provides implementations of both rectangular and semicircle [parliament plots](https://github.com/andrewtavis/poli-sci-kit/blob/main/src/poli_sci_kit/plot/parliament.py):\n\n```python\nfig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)\n\nax1 = poli_sci_kit.plot.parliament(\n    allocations=seat_allocations,\n    labels=parties,\n    colors=party_colors,\n    style=\"rectangle\",\n    num_rows=4,\n    marker_size=300,\n    speaker=True,\n    axis=ax1,\n)\n\nax2 = poli_sci_kit.plot.parliament(\n    allocations=seat_allocations,\n    labels=parties,\n    colors=party_colors,\n    style=\"semicircle\",\n    num_rows=4,\n    marker_size=175,\n    speaker=False,\n    axis=ax2,\n)\n\nplt.show()\n```\n\n\u003cp align=\"middle\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/andrewtavis/poli-sci-kit/main/.github/resources/images/rectangle_parliament.png\" width=\"400\" /\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/andrewtavis/poli-sci-kit/main/.github/resources/images/semicircle_parliament.png\" width=\"400\" /\u003e\n\u003c/p\u003e\n\n\u003ca id=\"disproportionality-bar-plot\"\u003e\u003c/a\u003e\n\n### Disproportionality Bar Plot [`⇧`](#contents)\n\nA novel addition to social science analysis is the [disproportionality bar plot](https://github.com/andrewtavis/poli-sci-kit/blob/main/src/poli_sci_kit/plot/dispr_bar.py), which graphically depicts the disproportionality between expected and realized results. Bar widths are the proportion of shares (ex: votes received), and heights are the difference or relative difference between shares and allocations (ex: parliament seats received).\n\nAn example follows:\n\n```python\nimport pltviz\n\nax = poli_sci_kit.plot.dispr_bar(\n    shares=votes,\n    allocations=ha_allocations,\n    labels=parties,\n    colors=party_colors,\n    total_shares=None,\n    total_alloc=None,\n    percent=True,\n    axis=None,\n)\n\nhandles, labels = pltviz.plot.legend.gen_elements(\n    counts=[round(v / sum(votes), 4) for v in votes],\n    labels=parties,\n    colors=party_colors,\n    size=11,\n    marker=\"o\",\n    padding_indexes=None,\n    order=None,\n)\n\nax.legend(\n    handles=handles,\n    labels=labels,\n    title=\"Vote Percents (bar widths)\",\n    title_fontsize=15,\n    fontsize=11,\n    ncol=2,\n    loc=\"upper left\",\n    bbox_to_anchor=(0, 1),\n    frameon=True,\n    facecolor=\"#FFFFFF\",\n    framealpha=1,\n)\n\nax.axes.set_title('Seat to Vote Share Disproportionality', fontsize=30)\nax.set_xlabel('Parties', fontsize=20)\nax.set_ylabel('Percent Shift', fontsize=20)\n\nplt.show()\n```\n\n\u003cp align=\"middle\"\u003e\n  \u003cimg src=\"https://raw.githubusercontent.com/andrewtavis/poli-sci-kit/main/.github/resources/images/dispr_bar.png\" width=\"600\" /\u003e\n\u003c/p\u003e\n\n\u003ca id=\"examples\"\u003e\u003c/a\u003e\n\n# Examples [`⇧`](#contents)\n\nExamples in poli-sci-kit use publicly available Wikidata statistics sourced via the Python package [wikirepo](https://github.com/andrewtavis/wikirepo). Current examples include:\n\n- [US HoR](https://github.com/andrewtavis/poli-sci-kit/blob/main/examples/us_house_of_rep.ipynb) [(Open in Colab)](https://colab.research.google.com/github/andrewtavis/poli-sci-kit/blob/main/examples/us_house_of_rep.ipynb)\n\n  - Allocates seats to a version of the US House of Representatives that includes all US territories and Washington DC given census data, with this further being used to derive relative vote strengths of state citizens in the US presidential election\n\n- [Global Parliament](https://github.com/andrewtavis/poli-sci-kit/blob/main/examples/global_parliament.ipynb) [(Open in Colab)](https://colab.research.google.com/github/andrewtavis/poli-sci-kit/blob/main/examples/global_parliament.ipynb)\n  - Analyzes the allocation of seats in a hypothetical global parliament given the prevalence of certain countries and organizations, the distribution of seats based on Freedom House indexes, as well as disproportionality metrics\n\n\u003ca id=\"to-do\"\u003e\u003c/a\u003e\n\n# To-Do [`⇧`](#contents)\n\nPlease see the [contribution guidelines](https://github.com/andrewtavis/poli-sci-kit/blob/main/.github/CONTRIBUTING.md) if you are interested in contributing to this project. Work that is in progress or could be implemented includes:\n\n- Adding the [Adams method](https://en.wikipedia.org/wiki/Highest_averages_method) to [appointment.methods.highest_averages](https://github.com/andrewtavis/poli-sci-kit/blob/main/src/poli_sci_kit/appointment/methods.py) ([see issue](https://github.com/andrewtavis/poli-sci-kit/issues/21))\n\n- Deriving further needed arguments to assure that all current and historic appointment systems can be simulated using poli-sci-kit ([see issue](https://github.com/andrewtavis/poli-sci-kit/issues/22))\n\n- Potentially indexing preset versions of [appointment.methods](https://github.com/andrewtavis/poli-sci-kit/blob/main/src/poli_sci_kit/appointment/methods.py) that coincide with the systems used by governments around the world\n\n  - This would allow quick comparisons of actual systems with variations\n\n- Adding methods such as quadratic voting to poli-sci-kit to allow for preference based simulations\n\n- Creating, improving and sharing [examples](https://github.com/andrewtavis/poli-sci-kit/tree/main/examples)\n\n- Improving [tests](https://github.com/andrewtavis/poli-sci-kit/tree/main/tests) for greater [code coverage](https://codecov.io/gh/andrewtavis/poli-sci-kit)\n\n# References\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eFull list of references\u003c/strong\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n- [voting](https://github.com/crflynn/voting) by [crflynn](https://github.com/crflynn) ([License](https://github.com/crflynn/voting/blob/master/LICENSE.txt))\n\n- https://blogs.reading.ac.uk/readingpolitics/2015/06/29/electoral-disproportionality-what-is-it-and-how-should-we-measure-it/\n\n- Balinski, M. L., and Young, H. P. (1982). Fair Representation: Meeting the Ideal of One Man, One Vote. New Haven, London: Yale University Press.\n\n- Karpov, A. (2008). \"Measurement of disproportionality in proportional representation systems\". Mathematical and Computer Modelling, Vol. 48, pp. 1421-1438. URL: https://www.sciencedirect.com/science/article/pii/S0895717708001933.\n\n- Kohler, U., and Zeh, J. (2012). “Apportionment methods”. The Stata Journal, Vol. 12, No. 3, pp. 375–392. URL: https://journals.sagepub.com/doi/pdf/10.1177/1536867X1201200303.\n\n- Taagepera, R., and Grofman, B. (2003). \"Mapping the Indices of Seats-Votes Disproportionality and Inter-Election Volatility\". Party Politics, Vol. 9, No. 6, pp. 659–677. URL: https://escholarship.org/content/qt0m9912ff/qt0m9912ff.pdf.\n\n\u003c/p\u003e\n\u003c/details\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrewtavis%2Fpoli-sci-kit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fandrewtavis%2Fpoli-sci-kit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fandrewtavis%2Fpoli-sci-kit/lists"}