-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.py
87 lines (74 loc) · 2.56 KB
/
update.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
# Created by: Era Iyer
# June 2020
#
# program purposes: determines appropriate color representation based on data
# regarding daily new coronavirus cases
#
# winning = green
# nearly there = orange
# needs action = red
#
# methods to determine color representation:
# 1. green --> current average < 10 OR current average < 20 and current avrage < 0.5*peak
# 2. orange --> current average < 1.5*20 and current average < peak*0.5
# OR current average < peak*0.2
# 3. red --> all other cases
import json
import csv
import pandas as pd
#parallel arrays to store json information
province = []
values = []
countries = []
setColors = []
allWeekValues = []
peakCases = []
all_moving_averages = []
F0 = 0.5
F1 = 0.2
N0 = 20
colors = ["green", "orange", "red"]
daysToAverage = 7
with open('./result.json') as f:
data = json.load(f)
for i in data:
#filling parallel arrays with province, values, country
province.append(i['state'])
values.append(i['new_cases'])
all_moving_averages.append(i['avg_cases'])
#another parallel array that stores each province's max number of cases
for i in range(len(all_moving_averages)):
max = 0
for j in range(len(all_moving_averages[i])):
if(all_moving_averages[i][j]>max):
max = all_moving_averages[i][j]
peakCases.append(max)
#simplifies values array to hold just values from the past week (per province/state)
for k in range(len(values)):
#gets values from last 7 days
start = len(values[k])-daysToAverage
end = len(values[k])
stateWeekVals = []
if(start < 0): #edge case, if data has less than 7 values
start = 0
for i in range(start, end):
stateWeekVals.append(values[k][i])
#array of array of new cases per week per state
allWeekValues.append(stateWeekVals)
for i in range(len(allWeekValues)):
sum = 0
for k in range(len(allWeekValues[i])):
sum += allWeekValues[i][k]
average = sum/daysToAverage
if((average < (N0 * F0)) or ((average < N0) and (average < (peakCases[i]*F0)))):
setColors.append(colors[0])
elif(((average < (1.5*N0)) and (average < (peakCases[i]*F0))) or (average < (peakCases[i]*F1))):
setColors.append(colors[1])
else:
setColors.append(colors[2])
with open('USStateColors.csv', 'w', newline='') as file:
fieldnames = ['state', 'color']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for i in range(len(province)):
writer.writerow({'state': province[i], 'color': setColors[i]})