-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcovid.py
173 lines (147 loc) · 5.65 KB
/
covid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import csv
import json
import re
from datetime import date, datetime
# master data list
data = {
'states': {}
}
def checkState(object, state):
if state not in data['states']:
stateObject = {
'cases': [],
'deaths': [],
'averages': [],
'scaledCases': [],
'scaledAverages': [],
'population': 0,
'name': state
}
data['states'][state] = stateObject
def addCases(row, state):
cases = int(row[3])
data['states'][state]['cases'].append(cases)
data['states'][state]['scaledCases'].append(cases * 1.0 * 100 / data['states'][state]['population'])
def addDeaths(row, state):
deaths = int(row[4])
data['states'][state]['deaths'].append(deaths)
def setTotalDays():
totalDays = 0
for state in data['states']:
if len(data['states'][state]['cases']) > totalDays:
totalDays = len(data['states'][state]['cases'])
data['totalDays'] = totalDays
def prependZeros(state, statistic):
gap = data['totalDays'] - len(data['states'][state][statistic])
if gap > 0:
zeros = [0] * gap
data['states'][state][statistic] = zeros + data['states'][state][statistic]
def addAverages(state):
days = len(data['states'][state]['cases'])
for i in range(len(data['states'][state]['cases'])):
lastDayCases = data['states'][state]['cases'][i]
if i < 7:
firstDayCases = data['states'][state]['cases'][0]
averageNew = (lastDayCases - firstDayCases) * 1.0 / (i + 1)
else:
firstDayCases = data['states'][state]['cases'][i - 7]
averageNew = (lastDayCases - firstDayCases) * 1.0 / 7.0
data['states'][state]['averages'].append(averageNew)
data['states'][state]['scaledAverages'].append(averageNew * 1.0 * 100000 / data['states'][state]['population'])
def getMax(state, statistic):
max = 0
for entry in data['states'][state][statistic]:
if entry > max:
max = entry
name = 'max' + statistic[0].upper() + statistic[1:]
data['states'][state][name] = max
# run after prepending zeros so that arrays are normalized
def getUsAverages():
usAverageObject = {
'cases': [0] * data['totalDays'],
'deaths': [0] * data['totalDays'],
'averages': [0] * data['totalDays'],
'scaledCases': [0] * data['totalDays'],
'scaledAverages': [0] * data['totalDays'],
'population': 0,
'name': 'US Average'
}
# get totals
for state in data['states']:
usAverageObject['population'] += data['states'][state]['population']
for i in range(0, data['totalDays']):
usAverageObject['cases'][i] += data['states'][state]['cases'][i]
usAverageObject['averages'][i] += data['states'][state]['averages'][i]
# scale totals
for i in range(0, data['totalDays']):
usAverageObject['scaledCases'][i] = usAverageObject['cases'][i] * 100.0 / usAverageObject['population']
usAverageObject['scaledAverages'][i] = usAverageObject['averages'][i] * 100000.0 / usAverageObject['population']
data['states']['US Average'] = usAverageObject
getMax('US Average', 'scaledCases')
getMax('US Average', 'scaledAverages')
def getMaxOverall(statistic):
max = 0
for state in data['states']:
if (statistic in data['states'][state] and data['states'][state][statistic] > max):
max = data['states'][state][statistic]
data[statistic] = max
# use population data to set up data object
with open('population.json') as populationFile:
populationData = json.load(populationFile)
for entry in populationData:
currentState = entry['region']
if entry['region'] not in data['states']:
checkState(data, currentState)
data['states'][currentState]['population'] = 0
data['states'][currentState]['population'] += int(entry['population'])
# parsing cases and deaths into data
with open('covid.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
next(readCSV)
lastDate = ""
for row in readCSV:
state = row[1]
addCases(row, state)
addDeaths(row, state)
lastDate = row[0]
data["lastDate"] = lastDate
# normalizing data and calculating averages
setTotalDays()
for state in sorted(data['states'].keys()):
prependZeros(state, 'cases')
prependZeros(state, 'scaledCases')
prependZeros(state, 'deaths')
addAverages(state)
getMax(state, 'cases')
getMax(state, 'deaths')
getMax(state, 'averages')
getMax(state, 'scaledCases')
getMax(state, 'scaledAverages')
getUsAverages()
getMaxOverall('maxCases')
getMaxOverall('maxDeaths')
getMaxOverall('maxAverages')
getMaxOverall('maxScaledCases')
getMaxOverall('maxScaledAverages')
# write object to file as JSON
with open('dist/assets/data.json', 'w') as output:
json.dump(data, output)
# set dates in html files
today = date.today()
todayString = today.strftime("%B %d, %Y")
searchTermToday = "<span id=\"date\">.*<\/span> with"
dateStringToday = "<span id=\"date\">" + todayString + "</span> with"
lastDateObject = datetime.strptime(data["lastDate"], "%Y-%m-%d")
dataString = lastDateObject.strftime("%B %d, %Y")
searchTermData = "<span id=\"lastData\">.*<\/span>"
dateStringData = "<span id=\"lastData\">" + dataString + "</span>"
def updateDate(filename):
inFile = open(filename, "rt")
inContents = inFile.read()
inContents = re.sub(searchTermToday, dateStringToday, inContents)
inContents = re.sub(searchTermData, dateStringData, inContents)
inFile.close()
outFile = open(filename, "wt")
outFile.write(inContents)
outFile.close()
updateDate("dist/index.html")