Added Double Rate Comparison Graph

This commit is contained in:
Patrick Müller 2020-04-11 23:36:57 +02:00
parent 1b8cabb2b5
commit 3a119a7d18
2 changed files with 49 additions and 1 deletions

View File

@ -401,3 +401,47 @@ class Analyser:
plt.close() plt.close()
return filePath return filePath
def getDoubleRateCompareGraph(self, country, plotDpi=200.0, showPlot=False) -> str:
fig = plt.figure()
fig.dpi = plotDpi
ax = fig.add_subplot(111)
plt.title('Double Rate Forecast vs Reality in ' + country)
data = self.df
mask = (data['countriesAndTerritories'] == country)
data = data.loc[mask]
data = data.sort_values('dateRep')
data['doubleRateForecast'] = data['totalCases'] / data['cases']
data['doubleRateReality'] = None
data = data.reset_index()
for index in data.index.values:
indexData = data.iloc[index]
# If there are cases in this country already
if indexData['totalCases'] > 0:
double = int(indexData['totalCases']) * 2
doubleDay = data.loc[(data['totalCases'] >= double)]
# If there is a day with double the value of cases
if len(doubleDay['dateRep'].unique()) > 0:
doubleDay = doubleDay.loc[(doubleDay['dateRep'] == doubleDay['dateRep'].unique()[0])]
indexDayDatetime = datetime.strptime(str(indexData['dateRep'])[:10], '%Y-%m-%d')
doubleDayDatetime = datetime.strptime(str(doubleDay['dateRep'].values)[2:12], '%Y-%m-%d')
difference = (doubleDayDatetime - indexDayDatetime).days
copyData = data.loc[(data['dateRep'] == indexData['dateRep'])]
copyData['doubleRateReality'] = difference
data.update(copyData)
data.plot(ax=ax, x='dateRep', y='doubleRateForecast')
data.plot(ax=ax, x='dateRep', y='doubleRateReality')
if showPlot:
plt.show(block=True)
filePath = ('graphs/doubleRateCompareGraph_' + country + '_' + datetime.now().strftime('%Y-%m-%d'))
fig.savefig(filePath)
plt.close()
return filePath

View File

@ -79,7 +79,8 @@ class UserInterface(QMainWindow):
self.graphTypePicker = QComboBox(parent=self) self.graphTypePicker = QComboBox(parent=self)
self.graphTypePicker.addItems( self.graphTypePicker.addItems(
['Total Cases', 'Case Increase', 'Increase Percentage', 'Cases per Million', 'Total Deaths', ['Total Cases', 'Case Increase', 'Increase Percentage', 'Cases per Million', 'Total Deaths',
'Death Increase', 'Death Rate', 'Deaths per Million', 'Is it going to end soon?']) 'Death Increase', 'Death Rate', 'Deaths per Million', 'Is it going to end soon?',
'Double Rate Compare'])
picklistBox.addWidget(self.graphTypePicker, 1) picklistBox.addWidget(self.graphTypePicker, 1)
# Calculate Button # Calculate Button
@ -167,6 +168,9 @@ class UserInterface(QMainWindow):
elif self.graphTypePicker.currentText() == 'Deaths per Million': elif self.graphTypePicker.currentText() == 'Deaths per Million':
deathsPerMillionGraphPath = self.analyser.getDeathsPerMillionGraph(country) deathsPerMillionGraphPath = self.analyser.getDeathsPerMillionGraph(country)
self.graphPlaceHolder.setPixmap(QPixmap(deathsPerMillionGraphPath)) self.graphPlaceHolder.setPixmap(QPixmap(deathsPerMillionGraphPath))
elif self.graphTypePicker.currentText() == 'Double Rate Compare':
doubleRateCompareGraphPath = self.analyser.getDoubleRateCompareGraph(country)
self.graphPlaceHolder.setPixmap(QPixmap(doubleRateCompareGraphPath))
def clearLayout(self, layout): def clearLayout(self, layout):
for i in reversed(range(layout.count())): for i in reversed(range(layout.count())):