From fd15b2bf87be2715fb2c6ddf92dc98684461bb1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20M=C3=BCller?= Date: Tue, 31 Mar 2020 23:31:35 +0200 Subject: [PATCH] :sparkles: Added logarhytmic graph that shows if the exponential growth has stopped --- Analyser.py | 40 ++++++++++++++++++++++++++++++---------- Main.py | 2 +- UserInterface.py | 29 +++++++++++++++++++++-------- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/Analyser.py b/Analyser.py index 35b3f45..f057247 100644 --- a/Analyser.py +++ b/Analyser.py @@ -8,6 +8,7 @@ import numpy as np import pandas as pd import matplotlib.pyplot as plt import tkinter as tk +import random """ Fields in csv: @@ -57,6 +58,7 @@ class Analyser: """ if country in self.getAvailableCountries(): fig = plt.figure() + fig.dpi = 200.0 ax = fig.add_subplot(111) plt.title(('Total cases in ' + country)) @@ -90,6 +92,7 @@ class Analyser: """ if country in self.getAvailableCountries(): fig = plt.figure() + fig.dpi = 200.0 ax = fig.add_subplot(111) plt.title(('Daily new cases in ' + country)) @@ -133,6 +136,7 @@ class Analyser: """ if country in self.getAvailableCountries(): fig = plt.figure() + fig.dpi = 200.0 ax = fig.add_subplot(111) plt.title(('Total deaths in ' + country)) @@ -166,6 +170,7 @@ class Analyser: """ if country in self.getAvailableCountries(): fig = plt.figure() + fig.dpi = 200.0 ax = fig.add_subplot(111) plt.title(('Daily new deaths in ' + country)) @@ -209,6 +214,7 @@ class Analyser: """ if country in self.getAvailableCountries(): fig = plt.figure() + fig.dpi = 200.0 ax = fig.add_subplot(111) plt.title(('Daily death rate in ' + country) + ' in %') @@ -245,17 +251,31 @@ class Analyser: countryTimeData = countryData.loc[mask] return (countryTimeData['deaths'].sum()/countryTimeData['cases'].sum()*100) - def testGraph(self, country, start_date='2019-12-31', end_date=datetime.now().strftime('%Y-%m-%d'), showPlot=False) -> str: - countryData = self.df[self.df['countriesAndTerritories'].isin([country])] - mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date) - countryTimeData = countryData.loc[mask] - countryTimeData = countryTimeData.sort_values('dateRep') - countryTimeData['totalCases'] = countryTimeData['cases'].cumsum() - countryTimeData['totalDeaths'] = countryTimeData['deaths'].cumsum() - countryTimeData['deathRate'] = countryTimeData['totalDeaths'] / countryTimeData['totalCases'] * 100 + def getIsItOverGraph(self, country, start_date='2019-12-31', end_date=datetime.now().strftime('%Y-%m-%d'), showPlot=False) -> str: + countryString = country + fig = plt.figure() + fig.dpi = 200.0 + ax = fig.add_subplot(111) + plt.title('Is it going to end soon?') + ax.set_ylabel('Case Increase') + ax.set_xlabel('Total Cases') + for index, country in enumerate([country, 'China', 'South_Korea'], start=1): + countryData = self.df[self.df['countriesAndTerritories'].isin([country])] + mask = (countryData['dateRep'] >= start_date) & (countryData['dateRep'] <= end_date) + countryTimeData = countryData.loc[mask] + countryTimeData = countryTimeData.sort_values('dateRep') + countryTimeData['totalCases'] = countryTimeData['cases'].cumsum() + countryTimeData[country] = countryTimeData['cases'].rolling(7).mean() - countryTimeData.plot.scatter(x='totalCases', y='cases', c='deaths') + try: + countryTimeData.plot(ax=ax, x='totalCases', y=country, loglog=True) + except: + print('Error occured') if showPlot: plt.show(block=True) - plt.close() \ No newline at end of file + filePath = ('graphs/isItOverGraph_' + countryString + '_' + datetime.now().strftime('%Y-%m-%d')) + fig.savefig(filePath) + plt.close() + + return filePath diff --git a/Main.py b/Main.py index 19b2a7e..bb0389d 100644 --- a/Main.py +++ b/Main.py @@ -10,5 +10,5 @@ import UserInterface as UI if __name__ == '__main__': fetcher.updateStatsFile() - ana.Analyser().testGraph('Germany', showPlot=True) + #ana.Analyser().getIsItOverGraph('Germany', showPlot=True) UI.main() diff --git a/UserInterface.py b/UserInterface.py index 2cbf1d3..b45ae0a 100644 --- a/UserInterface.py +++ b/UserInterface.py @@ -47,9 +47,13 @@ class UserInterface(QWidget): # Country Picker countryPickerLabel = QLabel('

Pick a country:

') labelBox.addWidget(countryPickerLabel) - self.countryPicker = QComboBox(parent=self) - self.countryPicker.addItems(countries) - self.countryPicker.setCurrentText('Germany') + # self.countryPicker = QComboBox(parent=self) + # self.countryPicker.addItems(countries) + # self.countryPicker.setCurrentText('Germany') + # picklistBox.addWidget(self.countryPicker, 1) + self.countryPicker = QLineEdit() + self.countryPicker.setCompleter(QCompleter(countries)) + self.countryPicker.setText('Germany') picklistBox.addWidget(self.countryPicker, 1) # Start Date Picker @@ -72,7 +76,8 @@ 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', 'Death Rate']) + self.graphTypePicker.addItems(['Total Cases', 'Case Increase', 'Total Deaths', 'Death Increase', 'Death Rate', + 'Is it going to end soon?']) picklistBox.addWidget(self.graphTypePicker, 1) # Calculate Button @@ -108,13 +113,18 @@ class UserInterface(QWidget): if self.calculateSingleCountryStats.isChecked(): # To reset the button self.calculateSingleCountryStats.toggle() - country = self.countryPicker.currentText() + if self.countryPicker.text() in self.analyser.getAvailableCountries(): + country = self.countryPicker.text() + else: + self.casesNumber.setText(('

Unknown country ' + self.countryPicker.text() + '' + + '. Please try again with the given options

')) + return startDate = self.startDatePicker.currentText() endDate = self.endDatePicker.currentText() self.casesNumber.setText( - ('

Statistics for ' + country - + ' as of ' + endDate + ': Total Cases: ' - + str(self.analyser.getTotalCases(country,endDate)) + ('

Statistics for ' + country + + ' as of ' + endDate + ': Total Cases: ' + + str(self.analyser.getTotalCases(country, endDate)) + ', Total Deaths: ' + str(self.analyser.getTotalDeaths(country, endDate)) + ', Death Rate: ' + str(self.analyser.getDeathRate(country, endDate))[:4] + '%

')) @@ -123,6 +133,7 @@ class UserInterface(QWidget): deathGraphPath = self.analyser.getDeathGraph(country, startDate, endDate) deathIncreaseGraphPath = self.analyser.getDeathIncreaseGraph(country, startDate, endDate) deathRateGraphPath = self.analyser.getDailyDeathRateGraph(country, startDate, endDate) + isItOverGraph = self.analyser.getIsItOverGraph(country, startDate, endDate) self.graphPlaceHolder = QLabel(self) self.clearLayout(self.graphBox) self.graphBox.addWidget(self.graphPlaceHolder) @@ -137,6 +148,8 @@ class UserInterface(QWidget): self.graphPlaceHolder.setPixmap(QPixmap(deathIncreaseGraphPath)) elif self.graphTypePicker.currentText() == 'Death Rate': self.graphPlaceHolder.setPixmap(QPixmap(deathRateGraphPath)) + elif self.graphTypePicker.currentText() == 'Is it going to end soon?': + self.graphPlaceHolder.setPixmap(QPixmap(isItOverGraph)) def clearLayout(self, layout): for i in reversed(range(layout.count())):