Death Rate Graph

- Also refactored the analyser to have better performance and easier access to required values
This commit is contained in:
Patrick Müller 2020-03-30 17:36:59 +02:00
parent 2f2e164e40
commit 4b7c6e6af2
2 changed files with 49 additions and 17 deletions

View File

@ -33,6 +33,10 @@ class Analyser:
self.df = pd.read_csv('statsfile.csv') self.df = pd.read_csv('statsfile.csv')
self.df['dateRep'] = pd.to_datetime(self.df['dateRep'], format='%d/%m/%Y') self.df['dateRep'] = pd.to_datetime(self.df['dateRep'], format='%d/%m/%Y')
self.df = self.df.sort_values('dateRep')
self.df['totalCases'] = self.df['cases'].cumsum()
self.df['totalDeaths'] = self.df['deaths'].cumsum()
self.df['deathRate'] = self.df['deaths'] / self.df['cases'] * 100
def getAvailableCountries(self): def getAvailableCountries(self):
return self.df['countriesAndTerritories'].unique() return self.df['countriesAndTerritories'].unique()
@ -56,15 +60,13 @@ class Analyser:
if country in self.getAvailableCountries(): if country in self.getAvailableCountries():
fig = plt.figure() fig = plt.figure()
ax = fig.add_subplot(111) ax = fig.add_subplot(111)
plt.title(('Total cases graph for ' + country)) plt.title(('Total cases in ' + country))
countryData = self.df[self.df['countriesAndTerritories'].isin([country])] countryData = self.df[self.df['countriesAndTerritories'].isin([country])]
mask = (countryData['dateRep'] > start_date) & (countryData['dateRep'] <= end_date) mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date)
countryTimeData = countryData.loc[mask] countryTimeData = countryData.loc[mask]
countryTimeData = countryTimeData.sort_values('dateRep')
countryTimeData['cases'] = countryTimeData['cases'].cumsum()
countryTimeData.plot(ax=ax, x='dateRep', y='cases') countryTimeData.plot(ax=ax, x='dateRep', y='totalCases')
plt.show(block=True) plt.show(block=True)
filePath = ('graphs/casesGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d')) filePath = ('graphs/casesGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d'))
@ -86,10 +88,10 @@ class Analyser:
if country in self.getAvailableCountries(): if country in self.getAvailableCountries():
fig = plt.figure() fig = plt.figure()
ax = fig.add_subplot(111) ax = fig.add_subplot(111)
plt.title(('Daily case increase graph for ' + country)) plt.title(('Daily new cases in ' + country))
countryData = self.df[self.df['countriesAndTerritories'].isin([country])] countryData = self.df[self.df['countriesAndTerritories'].isin([country])]
mask = (countryData['dateRep'] > start_date) & (countryData['dateRep'] <= end_date) mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date)
countryTimeData = countryData.loc[mask] countryTimeData = countryData.loc[mask]
countryTimeData.plot(ax=ax, x='dateRep', y='cases') countryTimeData.plot(ax=ax, x='dateRep', y='cases')
@ -126,18 +128,16 @@ class Analyser:
if country in self.getAvailableCountries(): if country in self.getAvailableCountries():
fig = plt.figure() fig = plt.figure()
ax = fig.add_subplot(111) ax = fig.add_subplot(111)
plt.title(('Total deaths graph for ' + country)) plt.title(('Total deaths in ' + country))
countryData = self.df[self.df['countriesAndTerritories'].isin([country])] countryData = self.df[self.df['countriesAndTerritories'].isin([country])]
mask = (countryData['dateRep'] > start_date) & (countryData['dateRep'] <= end_date) mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date)
countryTimeData = countryData.loc[mask] countryTimeData = countryData.loc[mask]
countryTimeData = countryTimeData.sort_values('dateRep')
countryTimeData['deaths'] = countryTimeData['deaths'].cumsum()
countryTimeData.plot(ax=ax, x='dateRep', y='deaths') countryTimeData.plot(ax=ax, x='dateRep', y='totalDeaths')
plt.show(block=True) plt.show(block=True)
filePath = ('graphs/casesGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d')) filePath = ('graphs/deathsGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d'))
fig.savefig(filePath) fig.savefig(filePath)
return filePath return filePath
@ -156,16 +156,44 @@ class Analyser:
if country in self.getAvailableCountries(): if country in self.getAvailableCountries():
fig = plt.figure() fig = plt.figure()
ax = fig.add_subplot(111) ax = fig.add_subplot(111)
plt.title(('Daily deaths graph for ' + country)) plt.title(('Daily new deaths in ' + country))
countryData = self.df[self.df['countriesAndTerritories'].isin([country])] countryData = self.df[self.df['countriesAndTerritories'].isin([country])]
mask = (countryData['dateRep'] > start_date) & (countryData['dateRep'] <= end_date) mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date)
countryTimeData = countryData.loc[mask] countryTimeData = countryData.loc[mask]
countryTimeData.plot(ax=ax, x='dateRep', y='deaths') countryTimeData.plot(ax=ax, x='dateRep', y='deaths')
plt.show(block=True) plt.show(block=True)
filePath = ('graphs/casesIncreaseGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d')) filePath = ('graphs/deathsIncreaseGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d'))
fig.savefig(filePath)
return filePath
else:
print('Unknown country')
return '-1'
def getDailyDeathRateGraph(self, country, start_date='2019-12-31', end_date=datetime.now().strftime('%Y-%m-%d')) -> str:
"""
Get a graph with the daily increase number of cases for the entered country
:param country: The country you wish to get the graph for
:param start_date: The start date of the graph
:param end_date: The end date of the graph
:return: The path for the picture of the graph
"""
if country in self.getAvailableCountries():
fig = plt.figure()
ax = fig.add_subplot(111)
plt.title(('Daily death rate in ' + country) + ' in %')
countryData = self.df[self.df['countriesAndTerritories'].isin([country])]
mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date)
countryTimeData = countryData.loc[mask]
countryTimeData.plot(ax=ax, x='dateRep', y='deathRate')
plt.show(block=True)
filePath = ('graphs/dailyDeathRateGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d'))
fig.savefig(filePath) fig.savefig(filePath)
return filePath return filePath

View File

@ -72,7 +72,7 @@ class UserInterface(QWidget):
graphTypePickerLabel = QLabel('<h3>Pick a graph type:</h3>') graphTypePickerLabel = QLabel('<h3>Pick a graph type:</h3>')
labelBox.addWidget(graphTypePickerLabel) labelBox.addWidget(graphTypePickerLabel)
self.graphTypePicker = QComboBox(parent=self) self.graphTypePicker = QComboBox(parent=self)
self.graphTypePicker.addItems(['Total Cases', 'Case Increase', 'Total Deaths', 'Death Increase']) self.graphTypePicker.addItems(['Total Cases', 'Case Increase', 'Total Deaths', 'Death Increase', 'Death Rate'])
picklistBox.addWidget(self.graphTypePicker, 1) picklistBox.addWidget(self.graphTypePicker, 1)
# Calculate Button # Calculate Button
@ -118,9 +118,11 @@ class UserInterface(QWidget):
caseIncreaseGraphPath = self.analyser.getCaseIncreaseGraph(country, startDate, endDate) caseIncreaseGraphPath = self.analyser.getCaseIncreaseGraph(country, startDate, endDate)
deathGraphPath = self.analyser.getDeathGraph(country, startDate, endDate) deathGraphPath = self.analyser.getDeathGraph(country, startDate, endDate)
deathIncreaseGraphPath = self.analyser.getDeathIncreaseGraph(country, startDate, endDate) deathIncreaseGraphPath = self.analyser.getDeathIncreaseGraph(country, startDate, endDate)
deathRateGraphPath = self.analyser.getDailyDeathRateGraph(country, startDate, endDate)
self.graphPlaceHolder = QLabel(self) self.graphPlaceHolder = QLabel(self)
self.clearLayout(self.graphBox) self.clearLayout(self.graphBox)
self.graphBox.addWidget(self.graphPlaceHolder) self.graphBox.addWidget(self.graphPlaceHolder)
if self.graphTypePicker.currentText() == 'Total Cases': if self.graphTypePicker.currentText() == 'Total Cases':
self.graphPlaceHolder.setPixmap(QPixmap(casesGraphPath)) self.graphPlaceHolder.setPixmap(QPixmap(casesGraphPath))
elif self.graphTypePicker.currentText() == 'Case Increase': elif self.graphTypePicker.currentText() == 'Case Increase':
@ -129,6 +131,8 @@ class UserInterface(QWidget):
self.graphPlaceHolder.setPixmap(QPixmap(deathGraphPath)) self.graphPlaceHolder.setPixmap(QPixmap(deathGraphPath))
elif self.graphTypePicker.currentText() == 'Death Increase': elif self.graphTypePicker.currentText() == 'Death Increase':
self.graphPlaceHolder.setPixmap(QPixmap(deathIncreaseGraphPath)) self.graphPlaceHolder.setPixmap(QPixmap(deathIncreaseGraphPath))
elif self.graphTypePicker.currentText() == 'Death Rate':
self.graphPlaceHolder.setPixmap(QPixmap(deathRateGraphPath))
def clearLayout(self, layout): def clearLayout(self, layout):
for i in reversed(range(layout.count())): for i in reversed(range(layout.count())):