-
Notifications
You must be signed in to change notification settings - Fork 6
/
by_demographics.py
280 lines (236 loc) · 11.9 KB
/
by_demographics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import streamlit as st
from PIL import Image
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from year_summary import plot_year_summary
def format_df_amount(df, category):
"""Group bail amount set by columns (retaining "empty" groups)."""
return df.groupby(['bail_year', category])['bail_amount_sum'].sum().unstack(fill_value=0).stack()
def format_df_type(df, category, flag):
"""Group bail types set by columns (retaining "empty" groups).
flag must be 'count' (for total number of people) or 'pct' (for percentage of people in a given category)"""
return df.groupby(['bail_year', category, 'bail_type'])[f'bail_type_{flag}'].sum().unstack(fill_value=0).stack()
@st.cache()
def load_race_data():
category = 'race_group'
df_bail_set = pd.read_csv('data/cleaned/app_race_bail_set.csv')
df_bail_type = pd.read_csv('data/cleaned/app_race_bail_type.csv')
df_bail_set = format_df_amount(df_bail_set, category)
df_bail_type_pct = format_df_type(df_bail_type, category, 'pct')
df_bail_type_count = format_df_type(df_bail_type, category, 'count')
return df_bail_set, df_bail_type_pct, df_bail_type_count
@st.cache()
def load_sex_data():
category = 'sex'
df_bail_set = pd.read_csv('data/cleaned/app_sex_bail_set.csv')
df_bail_type = pd.read_csv('data/cleaned/app_sex_bail_type.csv')
df_bail_set = format_df_amount(df_bail_set, category)
df_bail_type_pct = format_df_type(df_bail_type, category, 'pct')
df_bail_type_count = format_df_type(df_bail_type, category, 'count')
return df_bail_set, df_bail_type_pct, df_bail_type_count
@st.cache()
def load_age_data():
category = 'age_group'
df_bail_set = pd.read_csv('data/cleaned/app_age_bail_set.csv')
df_bail_type = pd.read_csv('data/cleaned/app_age_bail_type.csv')
df_bail_set = format_df_amount(df_bail_set, category)
df_bail_type_pct = format_df_type(df_bail_type, category, 'pct')
df_bail_type_count = format_df_type(df_bail_type, category, 'count')
return df_bail_set, df_bail_type_pct, df_bail_type_count
def app():
# ----------------------------------
# Year-end summary (included on every page)
# ----------------------------------
fig = plot_year_summary()
f_year = go.FigureWidget(fig)
st.plotly_chart(f_year)
# ----------------------------------
# Description
# ----------------------------------
st.title('Breakdown by Demographics')
st.write("How has bail set differed between **races, genders, and age groups**?")
# ----------------------------------
# Preprocessing
# ----------------------------------
years = [2020, 2021]
bail_types = ['Denied', 'Monetary', 'Nonmonetary', 'ROR', 'Unsecured']
races = ['Black', 'White', 'Other']
sexes = ['Male', 'Female']
age_groups = ['minor', '18 to 25', '26 to 64', '65+']
df_race_bail_set, df_race_bail_type, df_race_bail_type_count = load_race_data()
df_sex_bail_set, df_sex_bail_type, df_sex_bail_type_count = load_sex_data()
df_age_bail_set, df_age_bail_type, df_age_bail_type_count = load_age_data()
arr_race_pct_2020 = np.array([df_race_bail_type[2020][race] for race in races], dtype=object)
arr_race_pct_2021 = np.array([df_race_bail_type[2021][race] for race in races], dtype=object)
arr_race_count_2020 = np.array([df_race_bail_type_count[2020][race] for race in races], dtype=object)
arr_race_count_2021 = np.array([df_race_bail_type_count[2021][race] for race in races], dtype=object)
arr_sex_pct_2020 = np.array([df_sex_bail_type[2020][sex] for sex in sexes], dtype=object)
arr_sex_pct_2021 = np.array([df_sex_bail_type[2021][sex] for sex in sexes], dtype=object)
arr_sex_count_2020 = np.array([df_sex_bail_type_count[2020][sex] for sex in sexes], dtype=object)
arr_sex_count_2021 = np.array([df_sex_bail_type_count[2021][sex] for sex in sexes], dtype=object)
arr_age_pct_2020 = np.array([df_age_bail_type[2020][age] for age in age_groups], dtype=object)
arr_age_pct_2021 = np.array([df_age_bail_type[2021][age] for age in age_groups], dtype=object)
arr_age_count_2020 = np.array([df_age_bail_type_count[2020][age] for age in age_groups], dtype=object)
arr_age_count_2021 = np.array([df_age_bail_type_count[2021][age] for age in age_groups], dtype=object)
# ----------------------------------
# Interactive figure formatting
# ----------------------------------
dropdown_content = dict(active=0,
x=-0.25,
y=0.92,
xanchor='left',
yanchor='top',
buttons=list([
dict(label="2020",
method="update",
args=[{"visible": [True, True, True, True, True,
False, False, False, False, False]}]),
dict(label="2021",
method="update",
args=[{"visible": [False, False, False, False, False,
True, True, True, True, True]}])
])
)
dropdown_title = dict(text="Select year:",
x=-0.25,
y=1,
xref="paper",
yref="paper",
align="left",
showarrow=False)
common_layout = dict(margin={'t':25},
barmode='stack',
legend={'traceorder': 'normal'},
legend_title="Bail Types",
yaxis_title="Percent")
# ----------------------------------
# Interactive figure: Bail Type Percentages by Race
# ----------------------------------
st.subheader('Race')
st.write("In 2020, monetary bail was set more frequently for people identified by the court system as Black than those identified as non-Black (White or Other).")
fig = go.Figure()
for j, bailType in enumerate(bail_types):
bail_pct = arr_race_pct_2020[:, j]
bail_count = arr_race_count_2020[:, j]
fig.add_trace(go.Bar(
x=list(range(len(races))),
y=bail_pct,
orientation="v",
text="",
textposition="inside",
name=bailType,
hoverinfo="text",
hovertext=[f"{bailType}: {bailPct:.1f}% of 2020 total, {race} ({count:,d} people)"
for bailPct, race, count in zip(bail_pct, races, bail_count)]
))
for j, bailType in enumerate(bail_types):
bail_pct = arr_race_pct_2021[:, j]
bail_count = arr_race_count_2020[:, j]
fig.add_trace(go.Bar(
x=list(range(len(races))),
y=bail_pct,
orientation="v",
text="",
textposition="inside",
name=bailType,
hoverinfo="text",
hovertext=[f"{bailType}: {bailPct:.1f}% of 2021 total, {race} ({count:,d} people)"
for bailPct, race, count in zip(bail_pct, races, bail_count)],
visible=False
))
fig.update_layout(xaxis_title="Race",
xaxis_tickvals=list(range(len(races))),
xaxis_ticktext=races)
fig.update_layout(updatemenus=[dropdown_content],
annotations=[dropdown_title],
**common_layout)
f_pct = go.FigureWidget(fig)
st.plotly_chart(f_pct)
st.write("*Note*: While additional race categories beyond \"White\" and \"Black\" are recognized by the Pennsylvania court system, these are grouped together as \"Other\" out of anonymization concerns. The Philadelphia Bail Fund has observed that the Philadelphia court system appears to record most non-Black and non-Asian people, such as Latinx and Indigenous people, as White.")
# ----------------------------------
# Interactive figure: Bail Type Percentages by Sex
# ----------------------------------
st.subheader('Sex')
st.write("In 2020, monetary bail was set more frequently for people identified by the court system as male than those identified as female.")
fig = go.Figure()
for j, bailType in enumerate(bail_types):
bail_pct = arr_sex_pct_2020[:, j]
bail_count = arr_sex_count_2020[:, j]
fig.add_trace(go.Bar(
x=list(range(len(sexes))),
y=bail_pct,
orientation="v",
text="",
textposition="inside",
name=bailType,
hoverinfo="text",
hovertext=[f"{bailType}: {bailPct:.1f}% of 2020 total, {sex} ({count:,d} people)"
for bailPct, sex, count in zip(bail_pct, sexes, bail_count)]
))
for j, bailType in enumerate(bail_types):
bail_pct = arr_sex_pct_2021[:, j]
bail_count = arr_sex_count_2021[:, j]
fig.add_trace(go.Bar(
x=list(range(len(sexes))),
y=bail_pct,
orientation="v",
text="",
textposition="inside",
name=bailType,
hoverinfo="text",
hovertext=[f"{bailType}: {bailPct:.1f}% of 2021 total for {sex} ({count:,d} people)"
for bailPct, sex, count in zip(bail_pct, sexes, bail_count)],
visible=False
))
fig.update_layout(xaxis_title="Sex",
xaxis_tickvals=list(range(len(sexes))),
xaxis_ticktext=sexes)
fig.update_layout(updatemenus=[dropdown_content],
annotations=[dropdown_title],
**common_layout)
f_pct_sex = go.FigureWidget(fig)
st.plotly_chart(f_pct_sex)
# ----------------------------------
# Interactive figure: Bail Type Percentages by Age
# ----------------------------------
st.subheader('Age')
st.write("In 2020, the frequency of monetary bail decreased with increasing age group.")
fig = go.Figure()
for j, bailType in enumerate(bail_types):
bail_pct = arr_age_pct_2020[:, j]
bail_count = arr_age_count_2020[:, j]
fig.add_trace(go.Bar(
x=list(range(len(age_groups))),
y=bail_pct,
orientation="v",
text="",
textposition="inside",
name=bailType,
hoverinfo="text",
hovertext=[f"{bailType}: {bailPct:.1f}% of 2020 total, {age} ({count:,d} people)"
for bailPct, age, count in zip(bail_pct, age_groups, bail_count)]
))
for j, bailType in enumerate(bail_types):
bail_pct = arr_age_pct_2021[:, j]
bail_count = arr_age_count_2021[:, j]
fig.add_trace(go.Bar(
x=list(range(len(age_groups))),
y=bail_pct,
orientation="v",
text="",
textposition="inside",
name=bailType,
hoverinfo="text",
hovertext=[f"{bailType}: {bailPct:.1f}% of 2021 total for {age} ({count:,d} people)"
for bailPct, age, count in zip(bail_pct, age_groups, bail_count)],
visible=False
))
fig.update_layout(xaxis_title="Age Group",
xaxis_tickvals=list(range(len(age_groups))),
xaxis_ticktext=age_groups)
fig.update_layout(updatemenus=[dropdown_content],
annotations=[dropdown_title],
**common_layout)
f_pct_age = go.FigureWidget(fig)
st.plotly_chart(f_pct_age)