From 4b7c6e6af21c330a97a5d1a43254c9efcc5a98a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Mon, 30 Mar 2020 17:36:59 +0200 Subject: [PATCH] :sparkles: Death Rate Graph - Also refactored the analyser to have better performance and easier access to required values --- Analyser.py | 60 +++++++++++++++++++++++++++++++++++------------- UserInterface.py | 6 ++++- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/Analyser.py b/Analyser.py index d3b0a5b..934723f 100644 --- a/Analyser.py +++ b/Analyser.py @@ -33,6 +33,10 @@ class Analyser: self.df = pd.read_csv('statsfile.csv') 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): return self.df['countriesAndTerritories'].unique() @@ -56,15 +60,13 @@ class Analyser: if country in self.getAvailableCountries(): fig = plt.figure() 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])] - mask = (countryData['dateRep'] > start_date) & (countryData['dateRep'] <= end_date) + mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date) 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) filePath = ('graphs/casesGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d')) @@ -86,10 +88,10 @@ class Analyser: if country in self.getAvailableCountries(): fig = plt.figure() 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])] - mask = (countryData['dateRep'] > start_date) & (countryData['dateRep'] <= end_date) + mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date) countryTimeData = countryData.loc[mask] countryTimeData.plot(ax=ax, x='dateRep', y='cases') @@ -126,18 +128,16 @@ class Analyser: if country in self.getAvailableCountries(): fig = plt.figure() 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])] - mask = (countryData['dateRep'] > start_date) & (countryData['dateRep'] <= end_date) + mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date) 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) - filePath = ('graphs/casesGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d')) + filePath = ('graphs/deathsGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d')) fig.savefig(filePath) return filePath @@ -156,16 +156,44 @@ class Analyser: if country in self.getAvailableCountries(): fig = plt.figure() 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])] - mask = (countryData['dateRep'] > start_date) & (countryData['dateRep'] <= end_date) + mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date) countryTimeData = countryData.loc[mask] countryTimeData.plot(ax=ax, x='dateRep', y='deaths') 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) return filePath diff --git a/UserInterface.py b/UserInterface.py index 7954135..334ee00 100644 --- a/UserInterface.py +++ b/UserInterface.py @@ -72,7 +72,7 @@ class UserInterface(QWidget): graphTypePickerLabel = QLabel('

Pick a graph type:

') labelBox.addWidget(graphTypePickerLabel) 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) # Calculate Button @@ -118,9 +118,11 @@ class UserInterface(QWidget): caseIncreaseGraphPath = self.analyser.getCaseIncreaseGraph(country, startDate, endDate) deathGraphPath = self.analyser.getDeathGraph(country, startDate, endDate) deathIncreaseGraphPath = self.analyser.getDeathIncreaseGraph(country, startDate, endDate) + deathRateGraphPath = self.analyser.getDailyDeathRateGraph(country, startDate, endDate) self.graphPlaceHolder = QLabel(self) self.clearLayout(self.graphBox) self.graphBox.addWidget(self.graphPlaceHolder) + if self.graphTypePicker.currentText() == 'Total Cases': self.graphPlaceHolder.setPixmap(QPixmap(casesGraphPath)) elif self.graphTypePicker.currentText() == 'Case Increase': @@ -129,6 +131,8 @@ class UserInterface(QWidget): self.graphPlaceHolder.setPixmap(QPixmap(deathGraphPath)) elif self.graphTypePicker.currentText() == 'Death Increase': self.graphPlaceHolder.setPixmap(QPixmap(deathIncreaseGraphPath)) + elif self.graphTypePicker.currentText() == 'Death Rate': + self.graphPlaceHolder.setPixmap(QPixmap(deathRateGraphPath)) def clearLayout(self, layout): for i in reversed(range(layout.count())):