-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_youth.py
568 lines (485 loc) · 19 KB
/
data_youth.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import pandas as pd
import plotly.express as px
from plotly.subplots import make_subplots
import plotly.graph_objs as go
# Plots we can generate from this dataset:
# - Pie chart of Custodial and community supervision actual-in count with geo and date filter
# - Pie chart of Custodial and community supervision community supervision count with geo and date filter
# - Line chart of Probation and Incarceration rate with geo and date filter
# - Pie chart of correctional services, by initial entry status with geo and date filter
# - Bar chart of community_sentences as subpart of inital entry status
# - Comparison chart for youth admission and release to correctional services
# - Admissions to correctional services by gender (trend and distribution)
# - Admissions to correctional services by age
# - Admissions to correctional services by identitiy
# TODO: modify to Add callbacks in functions: ideas: year range selector; dropdown or map for geo and radio buttons for other data (like supervision-type)
def youth_in_correctional_services(
start_year, end_year, template, supervision_type="actual-in", geos=None
):
"""Pie chart of Custodial and community supervision actual-in count/community supervision count with GEOs and date filter"""
df = pd.read_csv("./dataset/youth/35100003.csv")
df = df[
(df["REF_DATE"].str[:4].astype(int) >= start_year)
& (df["REF_DATE"].str[5:].astype(int) <= end_year)
]
if geos is not None:
df = df[df["GEO"].isin(geos)]
else:
geos = ["All Provinces and territories"]
df = df[~df["GEO"].isin(["Provinces and Territories"])]
df_actual_in = df[
df["Custodial and community supervision"].str.contains(supervision_type)
]
fig = px.bar(
df_actual_in,
x="Custodial and community supervision",
y="VALUE",
color="GEO",
title=f"{supervision_type} Counts of Young Persons in Correctional Services ({start_year} to {end_year})",
labels={
"Custodial and community supervision": "",
"VALUE": "Count",
"REF_DATE": "Year",
},
hover_data={
"Custodial and community supervision": False,
"REF_DATE": True,
"VALUE": True,
},
category_orders={
"Custodial and community supervision": sorted(
df_actual_in["REF_DATE"].unique()
)
},
)
fig.update_layout(
annotations=[
go.layout.Annotation(
x=0.5,
y=1.25,
text=f"Selected Location: {', '.join(geos)}",
showarrow=False,
xref="paper",
yref="paper",
font=dict(size=14),
)
],
template=template,
)
return fig
# Usage: youth_in_correctional_services(start_year=2015, end_year=2020, supervision_type="actual-in", geo=["Alberta"]) or youth_in_correctional_services(start_year=2015, end_year=2020, supervision_type="community supervision")
# def youth_in_correctional_services_trend(geo):
# """Line chart of Probation and Incarceration rate with geo and date filter"""
# data = pd.read_csv("./dataset/youth/35100003.csv")
# filtered_data = data[
# (data["GEO"] == geo)
# & (
# (
# data["Custodial and community supervision"]
# == "Incarceration rates per 10,000 young persons"
# )
# | (
# data["Custodial and community supervision"]
# == "Probation rate per 10,000 young persons"
# )
# )
# ]
# # Drop rows with missing values
# filtered_data = filtered_data.dropna(subset=["VALUE"])
# fig = px.line(
# filtered_data,
# x="REF_DATE",
# y="VALUE",
# color="Custodial and community supervision",
# title=f"Incarceration and Probation rates in {geo}",
# )
# fig.update_layout(
# xaxis_rangeslider_visible=True
# ) # this slider is just for testing, actual implementation should be done using callbacks.
# # fig.show()
# return fig
# Usage: youth_in_correctional_services('Alberta')
def youth_in_correctional_services_trend_3d(start_year, end_year,template,rate_type="Incarceration", geos=None
):
"""3D Line chart of Incarceration or Probation rate with geo and date filter"""
data = pd.read_csv("./dataset/youth/35100003.csv")
data = data[
(data["REF_DATE"].str[:4].astype(int) >= start_year)
& (data["REF_DATE"].str[:4].astype(int) <= end_year)
]
# Filter data for the specified rate type
if rate_type == "Incarceration":
filtered_data = data[
data["Custodial and community supervision"]
== "Incarceration rates per 10,000 young persons"
]
elif rate_type == "Probation":
filtered_data = data[
data["Custodial and community supervision"]
== "Probation rate per 10,000 young persons"
]
else:
print("Invalid rate type")
return None
# Drop rows with missing values
filtered_data = filtered_data.dropna(subset=["VALUE"])
if geos is not None:
filtered_data = filtered_data[filtered_data["GEO"].isin(geos)]
# Create a 3D line graph with trend lines for each GEO
fig = go.Figure()
for geo in filtered_data["GEO"].unique():
if geo == "Northwest Territories including Nunavut":
continue
geo_data = filtered_data[filtered_data["GEO"] == geo]
fig.add_trace(
go.Scatter3d(
x=geo_data["REF_DATE"],
y=[geo] * len(geo_data),
z=geo_data["VALUE"],
mode="lines",
name=geo,
)
)
# Update the layout of the graph
fig.update_layout(
scene=dict(
xaxis_title="Year",
yaxis_title="GEO",
zaxis_title=f"{rate_type} rate per 10,000 young persons",
),
title=f"{rate_type} rates in different GEOs",
height=650,
margin=dict(l=0, r=0, b=0, t=30),
template=template,
)
return fig
# Usage: youth_in_correctional_services_trend_3d('Incarceration')
# Usage: youth_in_correctional_services_trend_3d('Probation',['Alberta', 'Ontario'])
def youth_commencing_correctional_services(start_year, end_year, template, geos=None):
"""
Pie chart and a bar chart showing the distribution of initial entry status and community sentences
for youth commencing correctional services in the specified time period and geographic regions.
"""
# Load dataset
df = pd.read_csv("./dataset/youth/35100004.csv")
df = df[
(df["REF_DATE"].str[:4].astype(int) >= start_year)
& (df["REF_DATE"].str[5:].astype(int) <= end_year)
]
if geos is not None:
df = df[df["GEO"].isin(geos)]
else:
geos = ["All Provinces and territories"]
df = df[~df["GEO"].isin(["Provinces and territories"])]
df["GEO"] = df["GEO"].replace(
[
"Ontario, Ministry of Children and Youth Services (MCYS)",
"Ontario, Ministry of Community Safety and Correctional Services (MCSCS)",
],
"Ontario",
)
# Filter relevant data for pie chart
relevant_statuses_pie = [
"Pre-trial detention",
"Secure custody",
"Custody and supervision (secure)",
"Young Offenders Act (YOA) (secure)",
"Open custody",
"Custody and supervision (open)",
"Young Offenders Act (YOA) (open)",
"Total community sentences",
]
df_pie = df[df["Initial entry status"].isin(relevant_statuses_pie)]
df_grouped_pie = df_pie.groupby(["GEO", "Initial entry status"]).sum().reset_index()
# Filter relevant data for bar chart
relevant_statuses_bar = [
"Intensive support and supervision",
"Deferred custody and supervision",
"Supervised probation",
"Other community sentences",
]
df_bar = df[df["Initial entry status"].isin(relevant_statuses_bar)]
df_grouped_bar = df_bar.groupby(["GEO", "Initial entry status"]).sum().reset_index()
# Create pie chart
fig_pie = px.pie(
df_grouped_pie,
values="VALUE",
names="Initial entry status",
title=f"Distribution of Initial Entry Status by GEO from {start_year} to {end_year}",
)
fig_pie.update_traces(textposition="inside", textinfo="percent+label")
# Create bar chart
fig_bar = px.bar(
df_grouped_bar,
x="Initial entry status",
y="VALUE",
title=f"Distribution of Community Sentences by Initial Entry Status and GEO from {start_year} to {end_year}",
hover_data=["GEO", "VALUE", "Initial entry status"],
)
# Create subplots
fig = make_subplots(
rows=1,
cols=2,
specs=[[{"type": "pie"}, {"type": "bar"}]],
subplot_titles=("Initial Entry Status", "Community Sentences"),
)
fig.add_trace(fig_pie.data[0], row=1, col=1)
fig.add_trace(fig_bar.data[0], row=1, col=2)
fig.update_layout(
title_text=f"Youth commencing correctional services, by initial entry status ({start_year} to {end_year})"
)
fig.update_layout(
annotations=[
go.layout.Annotation(
x=0.5,
y=1,
text=f"Selected Location: {', '.join(geos)}",
showarrow=False,
xref="paper",
yref="paper",
font=dict(size=14),
)
],
template=template,
)
return fig
# Usage: youth_commencing_correctional_services(1999, 2022) OR youth_commencing_correctional_services(1999, 2022,['Ontario','Alberta'])
def youth_admissions_and_releases_to_correctional_services(
start_year, end_year, template, geos=None
):
"""Comparison chart for youth admission and release to correctional services"""
# Read the data
df = pd.read_csv("./dataset/youth/35100005.csv")
df = df[
(df["REF_DATE"].str[:4].astype(int) >= start_year)
& (df["REF_DATE"].str[5:].astype(int) <= end_year)
]
if geos is not None:
df = df[df["GEO"].isin(geos)]
else:
geos = ["All Provinces and territories"]
df = df[~df["GEO"].isin(["Provinces and territories"])]
# Filter the data based on 'Youth admissions' and 'Youth releases' only
df = df[df["Admissions and releases"].isin(["Youth admissions", "Youth releases"])]
# Filter the data based on the relevant correctional service categories
relevant_categories = [
"Pre-trial detention",
"Secure custody",
"Custody and supervision (secure)",
"Young Offenders Act (YOA) (secure)",
"Open custody",
"Custody and supervision (open)",
"Young Offenders Act (YOA) (open)",
"Total community sentences",
]
df = df[df["Correctional services"].isin(relevant_categories)]
# Pivot the data to create separate columns for 'Youth admissions' and 'Youth releases'
df = df.pivot(
index=["Correctional services", "REF_DATE", "GEO"],
columns="Admissions and releases",
values="VALUE",
).reset_index()
# Create the bar plot
fig = px.bar(
df,
x="Correctional services",
y=["Youth admissions", "Youth releases"],
color_discrete_sequence=px.colors.qualitative.Pastel1,
hover_data=["GEO", "REF_DATE"],
labels={"variable": "Admission/Release", "value": "Number of Youth"},
)
# Set the plot title and axis labels
fig.update_layout(
title=f"Youth Admissions and Releases to Correctional Service from ({start_year} to {end_year})",
xaxis_title="Correctional Service Category",
yaxis_title="Number of Youth",
barmode="group",
)
fig.update_layout(
annotations=[
go.layout.Annotation(
x=0.5,
y=1.1,
text=f"Selected Location: {', '.join(geos)}",
showarrow=False,
xref="paper",
yref="paper",
font=dict(size=14),
)
],
template=template,
)
return fig
# Usage: youth_admissions_and_releases_to_correctional_services(1999,2005,['Alberta','Manitoba'])
def youth_gender_trends_and_pie(start_year, end_year, template, geos=None):
"""Admissions to correctional services by gender (trend and distribution)"""
# print('youth_gender_trends_and_pie:',start_year, end_year, template, geos)
df = pd.read_csv("./dataset/youth/35100006.csv", low_memory=False)
df = df[
(df["REF_DATE"].str[:4].astype(int) >= start_year)
& (df["REF_DATE"].str[5:].astype(int) <= end_year)
]
if geos is not None:
df = df[df["GEO"].isin(geos)]
else:
df = df[df["GEO"] == "Provinces and territories"]
colors = ["blue", "red", "green"]
# filter the dataframe for the required values
df_filtered = df[
(df["Correctional services"] == "Total correctional services")
& (df["Age at time of admission"] == "Total, admissions by age")
& (df["Sex"] != "Sex unknown")
]
# group by year and sex and sum the value column
df_grouped = df_filtered.groupby(["REF_DATE", "Sex"])["VALUE"].sum().reset_index()
# pivot the dataframe to get the values for each gender
df_pivot = df_grouped.pivot(index="REF_DATE", columns="Sex", values="VALUE")
# create the gender trend plot
fig1 = go.Figure()
fig1.add_trace(
go.Scatter(
x=df_pivot.index, y=df_pivot["Males"], name="Males", line_color="red"
)
)
fig1.add_trace(
go.Scatter(
x=df_pivot.index, y=df_pivot["Females"], name="Females", line_color="green"
)
)
fig1.add_trace(
go.Scatter(
x=df_pivot.index,
y=df_pivot["Total, admissions by sex"],
name="Total",
line_color="blue",
)
)
fig1.update_layout(
title="Gender Trends for Total Correctional Services",
xaxis_title="Year",
yaxis_title="Number of Admissions",
)
# create the pie chart
gender_counts = df_filtered.groupby("Sex")["VALUE"].sum()
fig2 = go.Figure(
data=[go.Pie(labels=gender_counts.index, values=gender_counts.values)]
)
fig2.update_layout(title="Gender Distribution")
# create the subplots figure
fig = make_subplots(rows=1, cols=2, specs=[[{"type": "scatter"}, {"type": "pie"}]])
fig.add_trace(fig1.data[0], row=1, col=1)
fig.add_trace(fig1.data[1], row=1, col=1)
fig.add_trace(fig1.data[2], row=1, col=1)
fig.update_layout(
template=template,
title=f"Gender trend of Youth Admissions to Correctional Services ({start_year}-{end_year})",
)
fig.update_layout(
annotations=[
go.layout.Annotation(
x=0.5,
y=1.15,
text=f"Selected Location: {', '.join(geos)} (sum)",
showarrow=False,
xref="paper",
yref="paper",
font=dict(size=14),
)
],
template=template,
)
fig.add_trace(fig2.data[0], row=1, col=2)
return fig
# Usage: youth_gender_trends_and_pie(1997,2005,['Alberta']) OR youth_gender_trends_and_pie(1997,2005) - for all locations
def youth_age_by_geo(start_year, end_year, template, geos=None):
"""Admissions to correctional services by age"""
df = pd.read_csv("./dataset/youth/35100006.csv", low_memory=False)
df["GEO"] = df["GEO"].replace(
[
"Ontario, Ministry of Children and Youth Services (MCYS)",
"Ontario, Ministry of Community Safety and Correctional Services (MCSCS)",
],
"Ontario",
)
df = df[
(df["REF_DATE"].str[:4].astype(int) >= start_year)
& (df["REF_DATE"].str[5:].astype(int) <= end_year)
]
if geos is not None:
df = df[df["GEO"].isin(geos)]
df_filtered = df[
(df["Correctional services"] == "Total correctional services")
& (df["Sex"] == "Total, admissions by sex")
]
df_filtered = df_filtered[
~df_filtered["Age at time of admission"].isin(["Total, admissions by age"])
]
grouped = (
df_filtered.groupby(["GEO", "Age at time of admission"])
.agg({"VALUE": "sum"})
.reset_index()
)
fig = px.bar(
grouped,
x="GEO",
y="VALUE",
color="Age at time of admission",
labels={"VALUE": "Number of admissions"},
)
fig.update_layout(
template=template,
title=f"Age at time of admission to Correctional Services ({start_year}-{end_year})",
)
return fig
# Usage: youth_age_by_geo(1999,2022) or youth_age_by_geo(1999,2022,['Ontario','Alberta','Manitoba','Provinces and territories'])
def youth_indigenous_vs_nonindigenous(start_year, end_year, template, geos=None):
df = pd.read_csv("./dataset/youth/35100007.csv", low_memory=False)
# Filter data based on the input parameters
df = df[
(df["REF_DATE"].str[:4].astype(int) >= start_year)
& (df["REF_DATE"].str[5:].astype(int) <= end_year)
]
if geos is not None:
df = df[df["GEO"].isin(geos)]
df["GEO"] = df["GEO"].replace(
[
"Ontario, Ministry of Children and Youth Services (MCYS)",
"Ontario, Ministry of Community Safety and Correctional Services (MCSCS)",
],
"Ontario",
)
df = df[df["Sex"] == "Total, admissions by sex"]
df = df[df["Correctional services"] == "Total correctional services"]
df = df[
df["Indigenous identity"].isin(
["Indigenous identity", "Non-Indigenous identity"]
)
]
# Pivot the data to create separate columns for Indigenous and Non-Indigenous admissions
df = df.pivot_table(
index=["GEO"], columns=["Indigenous identity"], values=["VALUE"], aggfunc=sum
)
# Rename the columns and reset the index
df.columns = [" ".join(col).strip() for col in df.columns.values]
df = df.reset_index()
# Calculate the total number of admissions for each GEO
df["total"] = df["VALUE Indigenous identity"] + df["VALUE Non-Indigenous identity"]
# Calculate the percentage of Indigenous and Non-Indigenous admissions for each GEO
df["% Indigenous identity"] = (df["VALUE Indigenous identity"] / df["total"]) * 100
df["% Non-Indigenous identity"] = (
df["VALUE Non-Indigenous identity"] / df["total"]
) * 100
# Create the plot
fig = px.bar(
df,
x="GEO",
y=["% Indigenous identity", "% Non-Indigenous identity"],
labels={"value": "% of admissions", "variable": "Indigenous identity"},
title=f"Indigenous vs Non-Indigenous Youth Admissions to Correctional Services ({start_year}-{end_year})",
color_discrete_sequence=px.colors.qualitative.Pastel,
)
fig.update_layout(template=template, barmode="stack", xaxis_tickangle=-45)
fig.update_traces(hovertemplate="%{y:.2f}%")
return fig
# Usage: youth_indigenous_vs_nonindigenous(2019, 2020, ['Ontario', 'Manitoba','Alberta'])
# Usage: youth_indigenous_vs_nonindigenous(1999, 2005)