poli-sci-kit 0.0.1__tar.gz → 2.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- poli_sci_kit-2.0.0/LICENSE.txt +28 -0
- poli_sci_kit-2.0.0/NOTICE.txt +60 -0
- poli_sci_kit-2.0.0/PKG-INFO +415 -0
- poli_sci_kit-2.0.0/README.md +374 -0
- poli_sci_kit-2.0.0/pyproject.toml +152 -0
- poli_sci_kit-2.0.0/setup.cfg +4 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit/__init__.py +3 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit/appointment/__init__.py +3 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit/appointment/checks.py +332 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit/appointment/methods.py +519 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit/appointment/metrics.py +518 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit/plot/__init__.py +4 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit/plot/disproportionality_bar_plot.py +153 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit/plot/parliament_plot.py +153 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit/utils.py +559 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit.egg-info/PKG-INFO +415 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit.egg-info/SOURCES.txt +19 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit.egg-info/requires.txt +19 -0
- poli_sci_kit-2.0.0/src/poli_sci_kit.egg-info/top_level.txt +1 -0
- poli_sci_kit-2.0.0/tests/test_utils.py +162 -0
- poli-sci-kit-0.0.1/PKG-INFO +0 -38
- poli-sci-kit-0.0.1/README.md +0 -28
- poli-sci-kit-0.0.1/poli_sci_kit/__init__.py +0 -4
- poli-sci-kit-0.0.1/poli_sci_kit/analysis_utils.py +0 -236
- poli-sci-kit-0.0.1/poli_sci_kit/appointment_methods.py +0 -321
- poli-sci-kit-0.0.1/poli_sci_kit/appointment_utils.py +0 -260
- poli-sci-kit-0.0.1/poli_sci_kit/general_utils.py +0 -231
- poli-sci-kit-0.0.1/poli_sci_kit.egg-info/PKG-INFO +0 -38
- poli-sci-kit-0.0.1/poli_sci_kit.egg-info/SOURCES.txt +0 -13
- poli-sci-kit-0.0.1/poli_sci_kit.egg-info/requires.txt +0 -4
- poli-sci-kit-0.0.1/poli_sci_kit.egg-info/top_level.txt +0 -5
- poli-sci-kit-0.0.1/setup.cfg +0 -7
- poli-sci-kit-0.0.1/setup.py +0 -31
- {poli-sci-kit-0.0.1 → poli_sci_kit-2.0.0/src}/poli_sci_kit.egg-info/dependency_links.txt +0 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020, the poli-sci-kit developers. All rights reserved.
|
|
4
|
+
|
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
|
7
|
+
|
|
8
|
+
* Redistributions of source code must retain the above copyright notice, this
|
|
9
|
+
list of conditions and the following disclaimer.
|
|
10
|
+
|
|
11
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
|
13
|
+
and/or other materials provided with the distribution.
|
|
14
|
+
|
|
15
|
+
* Neither the name of the copyright holder nor the names of its
|
|
16
|
+
contributors may be used to endorse or promote products derived from
|
|
17
|
+
this software without specific prior written permission.
|
|
18
|
+
|
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Notice
|
|
2
|
+
|
|
3
|
+
The poli-sci-kit developers (PSK) built the Python package "poli-sci-kit"
|
|
4
|
+
(SERVICE) as an open-source application. This SERVICE is provided by PSK
|
|
5
|
+
at no cost and is intended for use as is.
|
|
6
|
+
|
|
7
|
+
This notice policy (NOTICE) is used to inform those who use the
|
|
8
|
+
service (USERS) of the policies for the use of this SERVICE, as well
|
|
9
|
+
as make the policies of sourced code and data clear.
|
|
10
|
+
|
|
11
|
+
1. Third-Party Source Code
|
|
12
|
+
|
|
13
|
+
This SERVICE was based on third party code. All source code used in
|
|
14
|
+
the creation of this SERVICE comes from sources that allow its full
|
|
15
|
+
use in the manner done so by the SERVICE. This section lists the source
|
|
16
|
+
code on which the SERVICE was based as well as the coinciding licenses
|
|
17
|
+
of each.
|
|
18
|
+
|
|
19
|
+
1.1. voting by Christopher Flynn
|
|
20
|
+
Source code: https://github.com/crflynn/voting
|
|
21
|
+
License: https://github.com/crflynn/voting/blob/master/LICENSE.txt
|
|
22
|
+
|
|
23
|
+
MIT License
|
|
24
|
+
|
|
25
|
+
Copyright (c) 2018-2019 Christopher Flynn
|
|
26
|
+
|
|
27
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
28
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
29
|
+
in the Software without restriction, including without limitation the rights
|
|
30
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
31
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
32
|
+
furnished to do so, subject to the following conditions:
|
|
33
|
+
|
|
34
|
+
The above copyright notice and this permission notice shall be included in all
|
|
35
|
+
copies or substantial portions of the Software.
|
|
36
|
+
|
|
37
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
38
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
39
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
40
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
41
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
42
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
43
|
+
SOFTWARE.
|
|
44
|
+
|
|
45
|
+
2. Changes
|
|
46
|
+
|
|
47
|
+
This NOTICE is subject to change. Updates to this NOTICE will replace
|
|
48
|
+
all prior instances, and if deemed material will further be clearly
|
|
49
|
+
stated in the next applicable update to the SERVICE. PSK encourages
|
|
50
|
+
the USERS to familiarize themselves with any changes to this NOTICE.
|
|
51
|
+
|
|
52
|
+
3. Contact
|
|
53
|
+
|
|
54
|
+
If you have any questions, concerns, or suggestions about this NOTICE,
|
|
55
|
+
do not hesitate to visit https://github.com/andrewtavis/poli-sci-kit.
|
|
56
|
+
The person responsible for such inquiries is Andrew Tavis McAllister.
|
|
57
|
+
|
|
58
|
+
4. Effective Date
|
|
59
|
+
|
|
60
|
+
This NOTICE is effective as of the 3rd of June, 2022.
|
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: poli-sci-kit
|
|
3
|
+
Version: 2.0.0
|
|
4
|
+
Summary: Political elections, appointment, analysis and visualization in Python
|
|
5
|
+
Author-email: Andrew Tavis McAllister <andrew.t.mcallister@gmail.com>
|
|
6
|
+
Project-URL: Homepage, https://github.com/andrewtavis/poli-sci-kit
|
|
7
|
+
Keywords: politics,analysis,voting,democracy,political-science,elections,appointment,political-appointment-analysis,disproportionality
|
|
8
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Intended Audience :: Education
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Operating System :: OS Independent
|
|
19
|
+
Requires-Python: >=3.12
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE.txt
|
|
22
|
+
License-File: NOTICE.txt
|
|
23
|
+
Requires-Dist: colormath>=3.0.0
|
|
24
|
+
Requires-Dist: matplotlib>=3.10.9
|
|
25
|
+
Requires-Dist: numpy>=2.4.6
|
|
26
|
+
Requires-Dist: packaging>=26.2
|
|
27
|
+
Requires-Dist: pandas>=3.0.3
|
|
28
|
+
Requires-Dist: scipy>=1.17.1
|
|
29
|
+
Requires-Dist: seaborn>=0.13.2
|
|
30
|
+
Provides-Extra: dev
|
|
31
|
+
Requires-Dist: prek>=0.4.1; extra == "dev"
|
|
32
|
+
Requires-Dist: pytest>=9.0.3; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-cov>=7.1.0; extra == "dev"
|
|
34
|
+
Requires-Dist: ruff>=0.15.14; extra == "dev"
|
|
35
|
+
Requires-Dist: ty>=0.0.38; extra == "dev"
|
|
36
|
+
Provides-Extra: docs
|
|
37
|
+
Requires-Dist: m2r2>=0.3.4; extra == "docs"
|
|
38
|
+
Requires-Dist: numpydoc>=1.10.0; extra == "docs"
|
|
39
|
+
Requires-Dist: sphinx-rtd-theme>=3.1.0; extra == "docs"
|
|
40
|
+
Dynamic: license-file
|
|
41
|
+
|
|
42
|
+
<a id="top"></a>
|
|
43
|
+
|
|
44
|
+
<div align="center">
|
|
45
|
+
<a href="https://github.com/andrewtavis/poli-sci-kit"><img src="https://raw.githubusercontent.com/andrewtavis/poli-sci-kit/main/.github/resources/logo/poli-sci-kit_logo_transparent.png" height=250></a>
|
|
46
|
+
</div>
|
|
47
|
+
|
|
48
|
+
<ol></ol>
|
|
49
|
+
|
|
50
|
+
[](http://poli-sci-kit.readthedocs.io/en/latest/)
|
|
51
|
+
[](https://github.com/andrewtavis/poli-sci-kit/actions/workflows/ci_static_analysis.yaml)
|
|
52
|
+
[](https://github.com/andrewtavis/poli-sci-kit/actions/workflows/ci_pytest.yaml)
|
|
53
|
+
[](https://pypi.org/project/poli-sci-kit/)
|
|
54
|
+
[](https://pypi.org/project/poli-sci-kit/)
|
|
55
|
+
[](https://pypi.org/project/poli-sci-kit/)
|
|
56
|
+
[](https://github.com/andrewtavis/poli-sci-kit/blob/main/LICENSE.txt)
|
|
57
|
+
[](https://github.com/andrewtavis/poli-sci-kit/blob/main/.github/CODE_OF_CONDUCT.md)
|
|
58
|
+
[](https://colab.research.google.com/github/andrewtavis/poli-sci-kit)
|
|
59
|
+
|
|
60
|
+
### Political elections, appointment, analysis and visualization in Python
|
|
61
|
+
|
|
62
|
+
**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.
|
|
63
|
+
|
|
64
|
+
# Contents
|
|
65
|
+
|
|
66
|
+
- [Installation](#installation-)
|
|
67
|
+
- [Appointment](#appointment-)
|
|
68
|
+
- [Plotting](#plotting-)
|
|
69
|
+
- [Parliament Plots](#parliament-plots-)
|
|
70
|
+
- [Disproportionality Bar Plot](#disproportionality-bar-plot-)
|
|
71
|
+
- [Examples](#examples-)
|
|
72
|
+
- [Development environment](#development-environment-)
|
|
73
|
+
- [To-Do](#to-do-)
|
|
74
|
+
|
|
75
|
+
# Installation
|
|
76
|
+
|
|
77
|
+
`poli-sci-kit` is available for installation via [uv](https://docs.astral.sh/uv/) (recommended) or [pip](https://pypi.org/project/poli-sci-kit/).
|
|
78
|
+
|
|
79
|
+
### For Users
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
# Using uv (recommended - fast, Rust-based installer):
|
|
83
|
+
uv pip install poli-sci-kit
|
|
84
|
+
|
|
85
|
+
# Or using pip:
|
|
86
|
+
pip install poli-sci-kit
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### For Development Build
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
git clone https://github.com/andrewtavis/poli-sci-kit.git
|
|
93
|
+
cd poli-sci-kit
|
|
94
|
+
|
|
95
|
+
# With uv (recommended):
|
|
96
|
+
uv sync --all-extras # install all dependencies
|
|
97
|
+
source .venv/bin/activate # activate venv (macOS/Linux)
|
|
98
|
+
# .venv\Scripts\activate # activate venv (Windows)
|
|
99
|
+
|
|
100
|
+
# Or with pip:
|
|
101
|
+
python -m venv .venv # create virtual environment
|
|
102
|
+
source .venv/bin/activate # activate venv (macOS/Linux)
|
|
103
|
+
# .venv\Scripts\activate # activate venv (Windows)
|
|
104
|
+
pip install -e .
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
import poli_sci_kit
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
<sub><a href="#top">Back to top.</a></sub>
|
|
112
|
+
|
|
113
|
+
# Appointment
|
|
114
|
+
|
|
115
|
+
[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:
|
|
116
|
+
|
|
117
|
+
#### Largest Remainder: Hare, Droop, Hagenbach–Bischoff (incl Hamilton, Vinton, Hare–Niemeyer)
|
|
118
|
+
|
|
119
|
+
#### Highest Averages: Jefferson, Webster, Huntington-Hill
|
|
120
|
+
|
|
121
|
+
Arguments 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.
|
|
122
|
+
|
|
123
|
+
A basic example of political appointment using poli-sci-kit is:
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from poli_sci_kit import appointment
|
|
127
|
+
|
|
128
|
+
vote_counts = [2700, 900, 3300, 1300, 2150, 500]
|
|
129
|
+
seats_to_allocate = 50
|
|
130
|
+
|
|
131
|
+
# Huntington-Hill is the method used to allocate House of Representatives seats to US states.
|
|
132
|
+
ha_allocations = appointment.methods.highest_averages(
|
|
133
|
+
averaging_style="Huntington-Hill",
|
|
134
|
+
shares=vote_counts,
|
|
135
|
+
total_allocation=seats_to_allocate,
|
|
136
|
+
allocation_threshold=None,
|
|
137
|
+
min_alloc=1,
|
|
138
|
+
tie_break="majority",
|
|
139
|
+
majority_bonus=False,
|
|
140
|
+
modifier=None,
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
ha_allocations
|
|
144
|
+
# [26, 9, 37, 12, 23, 5]
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
We can then compute various metrics to derive disproportionality:
|
|
148
|
+
|
|
149
|
+
```python
|
|
150
|
+
# The Gallagher method is a measure of absolute difference similar to summing square residuals.
|
|
151
|
+
disproportionality = appointment.metrics.disproportionality_index(
|
|
152
|
+
shares=vote_counts,
|
|
153
|
+
allocations=ha_allocations,
|
|
154
|
+
metric_type='Gallagher'
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
disproportionality
|
|
158
|
+
# 0.01002
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
We can also check that the allocations pass the [quota condition](https://en.wikipedia.org/wiki/Quota_rule):
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
passes_qc = appointment.checks.quota_condition(
|
|
165
|
+
shares=vote_counts,
|
|
166
|
+
seats=ha_allocations
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
passes_qc
|
|
170
|
+
# True
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Allocation 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.
|
|
174
|
+
|
|
175
|
+
<sub><a href="#top">Back to top.</a></sub>
|
|
176
|
+
|
|
177
|
+
# Plotting
|
|
178
|
+
|
|
179
|
+
poli-sci-kit provides Python only implementations of common electoral plots.
|
|
180
|
+
|
|
181
|
+
Visualizing the above results:
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
import matplotlib.pyplot as plt
|
|
185
|
+
import poli_sci_kit
|
|
186
|
+
|
|
187
|
+
# German political parties.
|
|
188
|
+
parties = ['CDU/CSU', 'FDP', 'Greens', 'Die Linke', 'SPD', 'AfD']
|
|
189
|
+
party_colors = ['#000000', '#ffed00', '#64a12d', '#be3075', '#eb001f', '#009ee0']
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
<sub><a href="#top">Back to top.</a></sub>
|
|
193
|
+
|
|
194
|
+
### Parliament Plots
|
|
195
|
+
|
|
196
|
+
poli_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_plot.py):
|
|
197
|
+
|
|
198
|
+
```python
|
|
199
|
+
fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
|
|
200
|
+
|
|
201
|
+
ax1 = poli_sci_kit.plot.parliament_plot(
|
|
202
|
+
allocations=seat_allocations,
|
|
203
|
+
labels=parties,
|
|
204
|
+
colors=party_colors,
|
|
205
|
+
style="rectangle",
|
|
206
|
+
num_rows=4,
|
|
207
|
+
marker_size=300,
|
|
208
|
+
speaker=True,
|
|
209
|
+
axis=ax1,
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
ax2 = poli_sci_kit.plot.parliament_plot(
|
|
213
|
+
allocations=seat_allocations,
|
|
214
|
+
labels=parties,
|
|
215
|
+
colors=party_colors,
|
|
216
|
+
style="semicircle",
|
|
217
|
+
num_rows=4,
|
|
218
|
+
marker_size=175,
|
|
219
|
+
speaker=False,
|
|
220
|
+
axis=ax2,
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
plt.show()
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
<p align="middle">
|
|
227
|
+
<img src="https://raw.githubusercontent.com/andrewtavis/poli-sci-kit/main/.github/resources/images/rectangle_parliament_plot.png" width="400" />
|
|
228
|
+
<img src="https://raw.githubusercontent.com/andrewtavis/poli-sci-kit/main/.github/resources/images/semicircle_parliament_plot.png" width="400" />
|
|
229
|
+
</p>
|
|
230
|
+
|
|
231
|
+
<sub><a href="#top">Back to top.</a></sub>
|
|
232
|
+
|
|
233
|
+
### Disproportionality Bar Plot
|
|
234
|
+
|
|
235
|
+
A 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/disproportionality_bar_plot.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).
|
|
236
|
+
|
|
237
|
+
An example follows:
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
import pltviz
|
|
241
|
+
|
|
242
|
+
ax = poli_sci_kit.plot.disproportionality_bar_plot(
|
|
243
|
+
shares=votes,
|
|
244
|
+
allocations=ha_allocations,
|
|
245
|
+
labels=parties,
|
|
246
|
+
colors=party_colors,
|
|
247
|
+
total_shares=None,
|
|
248
|
+
total_allocation=None,
|
|
249
|
+
percent=True,
|
|
250
|
+
axis=None,
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
handles, labels = pltviz.plot.legend.gen_elements(
|
|
254
|
+
counts=[round(v / sum(votes), 4) for v in votes],
|
|
255
|
+
labels=parties,
|
|
256
|
+
colors=party_colors,
|
|
257
|
+
size=11,
|
|
258
|
+
marker="o",
|
|
259
|
+
padding_indexes=None,
|
|
260
|
+
order=None,
|
|
261
|
+
)
|
|
262
|
+
|
|
263
|
+
ax.legend(
|
|
264
|
+
handles=handles,
|
|
265
|
+
labels=labels,
|
|
266
|
+
title="Vote Percents (bar widths)",
|
|
267
|
+
title_fontsize=15,
|
|
268
|
+
fontsize=11,
|
|
269
|
+
ncol=2,
|
|
270
|
+
loc="upper left",
|
|
271
|
+
bbox_to_anchor=(0, 1),
|
|
272
|
+
frameon=True,
|
|
273
|
+
facecolor="#FFFFFF",
|
|
274
|
+
framealpha=1,
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
ax.axes.set_title('Seat to Vote Share Disproportionality', fontsize=30)
|
|
278
|
+
ax.set_xlabel('Parties', fontsize=20)
|
|
279
|
+
ax.set_ylabel('Percent Shift', fontsize=20)
|
|
280
|
+
|
|
281
|
+
plt.show()
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
<p align="middle">
|
|
285
|
+
<img src="https://raw.githubusercontent.com/andrewtavis/poli-sci-kit/main/.github/resources/images/disproportionality_bar_plot.png" width="600" />
|
|
286
|
+
</p>
|
|
287
|
+
|
|
288
|
+
<sub><a href="#top">Back to top.</a></sub>
|
|
289
|
+
|
|
290
|
+
# Examples
|
|
291
|
+
|
|
292
|
+
Examples in poli-sci-kit use publicly available Wikidata statistics sourced via the Python package [wikirepo](https://github.com/andrewtavis/wikirepo). Current examples include:
|
|
293
|
+
|
|
294
|
+
- [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)
|
|
295
|
+
- 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
|
|
296
|
+
|
|
297
|
+
- [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)
|
|
298
|
+
- 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
|
|
299
|
+
|
|
300
|
+
<sub><a href="#top">Back to top.</a></sub>
|
|
301
|
+
|
|
302
|
+
## Development environment
|
|
303
|
+
|
|
304
|
+
Please follow the steps below to set up your development environment for poli-sci-kit contributions.
|
|
305
|
+
|
|
306
|
+
### Clone repository
|
|
307
|
+
|
|
308
|
+
```bash
|
|
309
|
+
# Clone your fork of the repo into the current directory.
|
|
310
|
+
git clone https://github.com/<your-username>/poli-sci-kit.git
|
|
311
|
+
# Navigate to the newly cloned directory.
|
|
312
|
+
cd poli-sci-kit
|
|
313
|
+
# Assign the original repo to a remote called "upstream".
|
|
314
|
+
git remote add upstream https://github.com/andrewtavis/poli-sci-kit.git
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
- Now, if you run `git remote -v` you should see two remote repositories named:
|
|
318
|
+
- `origin` (forked repository)
|
|
319
|
+
- `upstream` (poli-sci-kit repository)
|
|
320
|
+
|
|
321
|
+
### Environment
|
|
322
|
+
|
|
323
|
+
Create a virtual environment for poli-sci-kit (Python `>=3.12`), activate it and install dependencies.
|
|
324
|
+
|
|
325
|
+
> [!NOTE]
|
|
326
|
+
> First, install `uv` if you don't already have it by following the [official installation guide](https://docs.astral.sh/uv/getting-started/installation/).
|
|
327
|
+
|
|
328
|
+
#### uv venv
|
|
329
|
+
|
|
330
|
+
```bash
|
|
331
|
+
uv sync --all-extras # create .venv and install all dependencies from uv.lock
|
|
332
|
+
|
|
333
|
+
# Unix or macOS:
|
|
334
|
+
source .venv/bin/activate
|
|
335
|
+
|
|
336
|
+
# Windows:
|
|
337
|
+
.venv\Scripts\activate.bat # .venv\Scripts\activate.ps1 (PowerShell)
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
#### Conda
|
|
341
|
+
|
|
342
|
+
Download [Anaconda](https://www.anaconda.com/download) if you don't have it installed already.
|
|
343
|
+
|
|
344
|
+
```bash
|
|
345
|
+
conda env create --file environment.yaml
|
|
346
|
+
conda activate poli-sci-kit-dev
|
|
347
|
+
uv pip install -r <(uv export --format requirements-txt) # install all dependencies from uv.lock
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
> [!NOTE]
|
|
351
|
+
> If you change dependencies in `pyproject.toml`, regenerate the lock file with the following command:
|
|
352
|
+
>
|
|
353
|
+
> ```bash
|
|
354
|
+
> uv lock # refresh uv.lock for reproducible installs
|
|
355
|
+
> ```
|
|
356
|
+
|
|
357
|
+
### pre-commit
|
|
358
|
+
|
|
359
|
+
After activating the virtual environment, set up [prek](https://prek.j178.dev/) for pre-commit hooks by running:
|
|
360
|
+
|
|
361
|
+
```bash
|
|
362
|
+
# In the project root:
|
|
363
|
+
prek install
|
|
364
|
+
|
|
365
|
+
# Then test the pre-commit hooks to see how it works:
|
|
366
|
+
uv run prek run --all-files
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
> [!NOTE]
|
|
370
|
+
> If you are having issues with prek and want to send along your changes regardless, you can ignore the pre-commit hooks via the following:
|
|
371
|
+
>
|
|
372
|
+
> ```bash
|
|
373
|
+
> git commit --no-verify -m "COMMIT_MESSAGE"
|
|
374
|
+
> ```
|
|
375
|
+
|
|
376
|
+
<sub><a href="#top">Back to top.</a></sub>
|
|
377
|
+
|
|
378
|
+
# To-Do
|
|
379
|
+
|
|
380
|
+
Please see the [contribution guidelines](https://github.com/andrewtavis/poli-sci-kit/blob/main/CONTRIBUTING.md) if you are interested in contributing to this project. Work that is in progress or could be implemented includes:
|
|
381
|
+
|
|
382
|
+
- 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))
|
|
383
|
+
|
|
384
|
+
- 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))
|
|
385
|
+
|
|
386
|
+
- 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
|
|
387
|
+
- This would allow quick comparisons of actual systems with variations
|
|
388
|
+
|
|
389
|
+
- Adding methods such as quadratic voting to poli-sci-kit to allow for preference based simulations
|
|
390
|
+
|
|
391
|
+
- Creating, improving and sharing [examples](https://github.com/andrewtavis/poli-sci-kit/tree/main/examples)
|
|
392
|
+
|
|
393
|
+
<sub><a href="#top">Back to top.</a></sub>
|
|
394
|
+
|
|
395
|
+
# References
|
|
396
|
+
|
|
397
|
+
<details><summary><strong>Full list of references</strong></summary>
|
|
398
|
+
<p>
|
|
399
|
+
|
|
400
|
+
- [voting](https://github.com/crflynn/voting) by [crflynn](https://github.com/crflynn) ([License](https://github.com/crflynn/voting/blob/master/LICENSE.txt))
|
|
401
|
+
|
|
402
|
+
- https://blogs.reading.ac.uk/readingpolitics/2015/06/29/electoral-disproportionality-what-is-it-and-how-should-we-measure-it/
|
|
403
|
+
|
|
404
|
+
- Balinski, M. L., and Young, H. P. (1982). Fair Representation: Meeting the Ideal of One Man, One Vote. New Haven, London: Yale University Press.
|
|
405
|
+
|
|
406
|
+
- 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.
|
|
407
|
+
|
|
408
|
+
- 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.
|
|
409
|
+
|
|
410
|
+
- 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.
|
|
411
|
+
|
|
412
|
+
</p>
|
|
413
|
+
</details>
|
|
414
|
+
|
|
415
|
+
<sub><a href="#top">Back to top.</a></sub>
|