-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid19_flu_deaths_analysis.py
141 lines (107 loc) · 4.24 KB
/
covid19_flu_deaths_analysis.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
from connection_pool import get_connection
import matplotlib.pyplot as plt
import numpy as np
MENU_PROMPT = """
---- Menu ----
1. Show COVID-19 Deaths per State (01/01/2020 - 06/26/2021) chart
2. Influenza:Pneumonia Deaths per State (2018-2019) chart
3. Show COVID-19 Deaths per State (01/01/2020 - 06/26/2021) and Influenza Deaths per State (2018-2019) chart
4. Exit
Enter your choice: """
SELECT_COVID19_DEATHS_PER_STATE = """SELECT usa_state, covid19_deaths
FROM covid19
ORDER BY usa_state;"""
SELECT_FLU_DEATHS_PER_STATE = """SELECT usa_state, SUM(deaths)
FROM influenza
GROUP BY usa_state
ORDER BY usa_state;"""
SELECT_COVID19_AND_FLU_DEATHS_PER_STATE = """SELECT covid19.usa_state, covid19.covid19_deaths,
(SELECT SUM(influenza.deaths) FROM influenza
WHERE covid19.usa_state = influenza.usa_state
GROUP BY influenza.usa_state) AS influenza_deaths
FROM covid19
ORDER BY usa_state;"""
def covid19deaths():
with get_connection() as connection:
with connection.cursor() as cursor:
cursor.execute(SELECT_COVID19_DEATHS_PER_STATE)
return cursor.fetchall()
def influenzadeaths():
with get_connection() as connection:
with connection.cursor() as cursor:
cursor.execute(SELECT_FLU_DEATHS_PER_STATE)
return cursor.fetchall()
def covid19fludeaths():
with get_connection() as connection:
with connection.cursor() as cursor:
cursor.execute(SELECT_COVID19_AND_FLU_DEATHS_PER_STATE)
return cursor.fetchall()
def create_chart_covid19():
covid19_deaths = covid19deaths()
figure = plt.figure(figsize=(16, 12))
axes = figure.add_subplot()
figure.subplots_adjust(bottom=0.07, top=0.95, left=0.07, right=0.95)
axes.set_title("COVID-19 Deaths per State (01/01/2020 - 06/26/2021)")
axes.set_ylabel("Number of Deaths")
axes.bar(
range(len(covid19_deaths)),
[covid19_death[1] for covid19_death in covid19_deaths],
tick_label=[covid19_death[0] for covid19_death in covid19_deaths],
facecolor="#4169E1",
edgecolor="#000000"
)
plt.yticks(np.arange(0, (max(covid19_deaths, key=lambda x: x[1])[1]), 1000))
def create_chart_flu():
flu_deaths = influenzadeaths()
figure = plt.figure(figsize=(15, 11))
axes = figure.add_subplot()
figure.subplots_adjust(bottom=0.07, top=0.95, left=0.07, right=0.95)
axes.set_title("Influenza:Pneumonia Deaths per State (2018-2019)")
axes.set_ylabel("Number of Deaths")
axes.bar(
range(len(flu_deaths)),
[flu_death[1] for flu_death in flu_deaths],
tick_label=[flu_death[0] for flu_death in flu_deaths],
facecolor="#FF8C00",
edgecolor="#000000"
)
plt.yticks(np.arange(0, (max(flu_deaths, key=lambda x: x[1])[1]), 500))
def create_chart_covid19_and_flu():
covid19_flu_deaths = covid19fludeaths()
figure = plt.figure(figsize=(21, 12.5))
axes = figure.add_subplot()
figure.subplots_adjust(bottom=0.07, top=0.95, left=0.05, right=0.98)
axes.set_title("COVID-19 (01/01/2020 - 06/26/2021) and Influenza:Pneumonia (2018-2019) Deaths per State (USA)")
axes.set_ylabel("Number of Deaths")
x_axis = np.arange(len(covid19_flu_deaths))
bar_width = 0.4
covid19_plot = axes.bar(
x_axis,
[covid19_flu_death[1] for covid19_flu_death in covid19_flu_deaths],
width=bar_width,
facecolor="#4169E1",
edgecolor="#000000"
)
influenza_plot = axes.bar(
x_axis + bar_width,
[covid19_flu_death[2] for covid19_flu_death in covid19_flu_deaths],
width=bar_width,
facecolor="#FF8C00",
edgecolor="#000000"
)
axes.legend((covid19_plot, influenza_plot), ("COVID-19 Deaths", "Influenza:Pneumonia Deaths"))
plt.xticks(x_axis + bar_width/2, [covid19_flu_death[0] for covid19_flu_death in covid19_flu_deaths])
plt.yticks(np.arange(0, (max(covid19_flu_deaths, key=lambda x: x[1])[1]), 1000))
MENU_OPTIONS = {
"1": create_chart_covid19,
"2": create_chart_flu,
"3": create_chart_covid19_and_flu
}
def menu():
while (selection := input(MENU_PROMPT)) != "4":
try:
MENU_OPTIONS[selection]()
plt.show()
except KeyError:
print("Invalid input selected. Please try again.")
menu()