Covid-19 Python App



Covid-19 Python App

With the recent developments with the current pandemic of SARS-CoV 2 it has become really difficult to find information about the data of the pandemic. All sources of media have been reporting different numbers of cases and have been incredibly inconsistent. Of course there are some wonderful resources such as the map compiled by Johns Hopkins University that shows the data very well. I, however, wanted to understand the data that provided a bit deeper so I developed this python app using the Covid-19 API produced by the before mentioned Johns Hopkins University.

So in the code uses the API to do a surface analysis of the data from a specific country the user requests. It returns data such as Number of confirmed cases, deaths, recoveries, active cases, threat level, and the mortality rate. I was especially displeased with the graphical representations of the pandemic in the news which used graphs of confirmed cases on a logarithmic scale so I made the code display a linear graph of active cases in a country.

Here is how the code runs:

Covid-19 is a highly infectious respiratory disease. This program can be used to get the current data about the disease.
For more information, visit: https://www.who.int/emergencies/diseases/novel-coronavirus-2019

General Overview of Covid-19
Number of Covid-19 cases worldwide: 4,791,229
Number of Covid-19 deaths worldwide: 321,071
Number of Covid-19 recoveries worldwide: 1,733,320
Number of active Covid-19 cases worldwide: 3,057,909
Current Covid-19 mortality rate: 6.7%

Which country would you like to find data for?(type STOP to exit the program) 

You would then input a country like "Slovakia" and hit enter resulting in the following output:

Slovakia:
Number of Covid-19 cases in Slovakia : 1,494
Number of Covid-19 deaths in Slovakia : 28
Number of Covid-19 recoveries in Slovakia : 1,163
Number of active Covid-19 cases in Slovakia : 331
Current Covid-19 mortality rate in Slovakia : 1.87%
The Healthcare system in Slovakia is managing the situation, please socially isolate.
Which country would you like to find data for?(type STOP to exit the program) 

So you might be surprised by how I evaluated the threat level and how the healthcare system in each respective country is doing. I just made up somewhat arbitrary numbers. It will be shown in the code what the mortality rates represent but here is a simple breakdown: above world mortality rate = very bad, below 3% = managing well, and then there are 2 categories between that which you can see in the code.


The code outputs this graph:


This graph shows the number of currently active cases in a country where 0. on the x-axis is the first day a case was reported in a country. This graph is more useful for countries such as Slovakia, Austria, and Czech Republic where the focus is no longer stopping the exponential growth of the virus but rather to decrease the number of cases as much as possible. It is understandable for countries where the virus is in an earlier stage and growing at an unprecedented rate to use a logarithmic scale graph as for those countries it is important to reduce the spread rather than reduce the number of infected. I found that more sources use these graphs however, and they don't show the advancements of the situation so I wanted to create something that does that too.

Here is the code: 
Unfortunately this blogging platform will not allow me to indent the code properly, it however isn't incredibly complicated so if you want to use it and are unsure about the indentation contact me.

#!/usr/bin/env python
import requests
import numpy as np
import matplotlib.pyplot as plt
#retrieving the API
response = requests.get("https://api.covid19api.com/")
api = requests.get('https://api.covid19api.com/summary').json()
overview = api['Global']
countries = np.array(api['Countries'])
#retrieving the specific data and processing it to get the mortality rates of specific countries.
total_confirmed = overview['TotalConfirmed']
total_deaths = overview['TotalDeaths']
total_recovered = overview['TotalRecovered']
total_active = total_confirmed - total_recovered
mortality_rate = (float(total_deaths) / float(total_confirmed)) * 100

#general overview of the situation with a link to the World Health Organization's website about Covid-19
print()
print("Covid-19 is a highly infectious respiratory disease. This program can be used to get the current data about the disease.")
print("For more information, visit: https://www.who.int/emergencies/diseases/novel-coronavirus-2019")
print()
print("General Overview of Covid-19")
print("Number of Covid-19 cases worldwide:",(format (total_confirmed, ',d')))
print("Number of Covid-19 deaths worldwide:",(format (total_deaths, ',d')))
print("Number of Covid-19 recoveries worldwide:",(format (total_recovered, ',d')))
print("Number of active Covid-19 cases worldwide:",(format (total_active, ',d')))
print("Current Covid-19 mortality rate:", str(round(mortality_rate,2)) + "%")
#this function retrieves the "threat level" in a country based on its and the world's mortality rate
def threat_level(country_mortality_rate, country):
if country_mortality_rate > mortality_rate:
print("!!!The Healthcare system in " + country + " is overwhelmed please quarantine at all costs!!!")
elif country_mortality_rate > 5:
print("The Healthcare system in " + country + " is volatile, please stay in quarantine besides absolute necesities.")
elif country_mortality_rate > 3:
print("The Healthcare system in " + country + " is handling the situation adequately, please stay in quarantine as much as possible.")
else:
print("The Healthcare system in " + country + " is managing the situation, please socially isolate.")
#this function uses mathplotlib to graph the data
def get_country_graph(country):
country_name = country
country_name = country_name.lower()
country_name = country_name.replace(" ", "-")
country_cases = requests.get('https://api.covid19api.com/dayone/country/' + country_name).json()
x = []
y = []
days_from = 0
for day in country_cases:
y.append(days_from)
x.append(day['Active'])
days_from = days_from + 1
plt.plot(y, x)
plt.xlabel('Days since first case')
plt.xlabel('Active cases')
plt.title('Cases in ' + country)
plt.show()
#this function retrieved the data for a specific country the user has input
def get_country_info(country):
country_info = next(item for item in countries if item["Country"] == country)
country_confirmed = country_info['TotalConfirmed']
country_deaths = country_info['TotalDeaths']
country_recovered = country_info['TotalRecovered']
country_active = country_confirmed - country_recovered
country_mortality_rate = (float(country_deaths) / float(country_confirmed)) * 100

print("Number of Covid-19 cases in " + country + " :",(format (country_confirmed, ',d')))
print("Number of Covid-19 deaths in " + country + " :",(format (country_deaths, ',d')))
print("Number of Covid-19 recoveries in " + country + " :",(format (country_recovered, ',d')))
print("Number of active Covid-19 cases in " + country + " :",(format (country_active, ',d')))
print("Current Covid-19 mortality rate in " + country + " :", str(round(country_mortality_rate,2)) + "%")
threat_level(country_mortality_rate, country)
#loop which will ask the user for input until he stops it
#this uses a try and except method in order to avoid the code crashing if a country is missing from the API or the user input an invalid country name
user_input = " "
while user_input != "STOP":
user_input = input("Which country would you like to find data for?(type STOP to exit the program) ")
if(user_input == "STOP"):
break
else:
print()
print(user_input + ":")
try:
get_country_info(user_input)
get_country_graph(user_input)
except:
print(user_input + " is not a valid country. Please enter a valid country.")
print()




Komentáre