-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPostProcessing.py
More file actions
257 lines (210 loc) · 10.1 KB
/
PostProcessing.py
File metadata and controls
257 lines (210 loc) · 10.1 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"""
Copyright (C) 2020 Eili Klein
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import time
#import matplotlib.pyplot as plt
#from matplotlib import cm
import numpy as np
import pandas as pd
import ParameterSet
import Utils
import os
def CompileResults(resultsName,modelPopNames,RegionalList,timeRange):
results = {}
for tend in timeRange:
results[tend] = {}
for i in range(0,len(RegionalList)):
regionVals = Utils.PickleFileRead(os.path.join(ParameterSet.PopDataFolder,str(modelPopNames)+str(i)+"RegionStats.pickle"))
for tend in timeRange:
if tend in regionVals:
dayVals = regionVals[tend]
for day in dayVals.keys():
rdict = dayVals[day]
results[tend][i]=rdict
return results
def WriteFitvals(resultsName,model,SLSH, SLSD, SLSC, avgperdiffhosp, avgperdiffdeaths, avgperdiffcases,writefolder=''):
if writefolder == '':
writefolder = ParameterSet.ResultsFolder
csvFile = writefolder+"/FittedResults_"+model+"_"+resultsName+".csv"
try:
fileexistsalready = False
if os.path.exists(csvFile):
fileexistsalready = True
with open(csvFile, 'a+') as f:
if not fileexistsalready:
f.write("SLSH, SLSD, SLSC, avgperdiffhosp, avgperdiffdeaths, avgperdiffcases\n")
f.write("%s,%s,%s,%s,%s,%s\n" % (SLSH, SLSD, SLSC, avgperdiffhosp, avgperdiffdeaths, avgperdiffcases))
except IOError:
print("I/O error")
def WriteParameterVals(resultsName,model,ParameterVals,writefolder=''):
if writefolder == '':
writefolder = ParameterSet.ResultsFolder
csvFile = writefolder+"/Parameters_"+model+"_"+resultsName+".csv"
try:
with open(csvFile, 'w') as f:
for key in ParameterVals.keys():
f.write("%s,%s\n" % (key,ParameterVals[key]))
f.write("\n")
except IOError:
print("I/O error")
def WriteAggregatedResults(results,model,resultsName,modelPopNames,RegionalList,HospitalNames=[],endTime=0,writefolder=''):
# add flag to write regional values
# add flag to write local values
print('Writing Results')
if writefolder == '':
writefolder = ParameterSet.ResultsFolder
### Get the results
maxday = 0
for d in results.keys():
daynow = d
if daynow > maxday:
maxday = daynow
#totdays = len(results.keys())
totdays = maxday
csvFile = writefolder+"/ResultsByDay_"+model+"_"+resultsName+".csv"
csvFileLocal = writefolder+"/LocalInfectedByDay_"+model+"_"+resultsName+".csv"
# This goes through the first row of results and gets the regional id - so results can be group by region. If none exists then we just give one set of results
regionalvals = []
localpopvals = []
numInfList = results[list(results.keys())[0]]
for reg in numInfList.keys():
rdict = numInfList[reg]
for rkey in rdict:
lpdict = rdict[rkey]
if 'regionalid' in lpdict.keys():
regionalval = lpdict['regionalid']
if not regionalval in regionalvals:
regionalvals.append(regionalval)
if 'localpopid' in lpdict.keys():
localpopval = lpdict['localpopid']
if not localpopval in localpopvals:
localpopvals.append(localpopval)
###########################################################################
# Write the regional data and totals for the main buckets out to disk
keyvals = ['S','N','I','C','R','D','H','HI','HE','ICU','numTests','vid0','vid1','InfEvtClear','CC']
colvals = ['Susceptible', 'Incubating', 'Infected', 'Colonized', 'Recovered', 'Dead', 'Hospitalized','NewAdmissions','EDVisits','ICU','Tests','Strain0','Strain1','InfectiousEventsPrevented','confirmedcases']
colvaltitles = []
if len(regionalvals) > 1:
for j in range(0,len(regionalvals)):
for i in range(0,len(colvals)):
colvaltitles.append(colvals[i]+"_"+str(regionalvals[j]).encode("ascii",errors="ignore").decode())
colvaltitles.extend(colvals)
else:
colvaltitles = list(colvals)
#set up output
output = np.empty((totdays,len(colvaltitles)+1),dtype=int)
# now go through the results and add the results as totals to each bucket
for day in results.keys():
if len(regionalvals) > 1:
resultdayvals = [0]*(len(colvals)*len(regionalvals)+len(colvals))
else:
resultdayvals = [0]*(len(colvals))
numInfList = results[day]
for reg in numInfList.keys():
rdict = numInfList[reg]
for rkey in rdict:
lpdict = rdict[rkey]
regionalval = lpdict['regionalid']
idx = regionalvals.index(regionalval)
st = idx*len(colvals)
totst = (len(regionalvals))*len(colvals)
keyon = 0
for i in range(st,st+(len(colvals))):
resultdayvals[i]+=lpdict[keyvals[keyon]]
keyon+=1
keyon = 0
if len(regionalvals) > 1:
for i in range(totst,totst+(len(colvals))):
resultdayvals[i]+=lpdict[keyvals[keyon]]
keyon+=1
output[(day - 1), :] = [day]+resultdayvals
titles = ['Day']
titles.extend(colvaltitles)
np.savetxt(csvFile,np.vstack([titles,output]),delimiter=",", fmt='%5s')
###########################################################################
# Write the local pop data and totals for I
#set up output
output = np.empty((totdays,len(localpopvals)+1),dtype=int)
# now go through the results and add the results as totals to each bucket
for day in results.keys():
resultdayvals = [0]*len(localpopvals)
numInfList = results[day]
for reg in numInfList.keys():
rdict = numInfList[reg]
for rkey in rdict:
lpdict = rdict[rkey]
localpopval = lpdict['localpopid']
idx = localpopvals.index(localpopval)
resultdayvals[idx]+=lpdict['I']
output[(day - 1), :] = [day]+resultdayvals
titles = ['Day']
for x in range(0,len(localpopvals)):
titles.append(str(localpopvals[x]).encode("ascii",errors="ignore").decode())
np.savetxt(csvFileLocal,np.vstack([titles,output]),delimiter=",", fmt='%5s')
##############################################################################
if ParameterSet.SaveHospitalData:
if len(HospitalNames) > 0:
hospstatsnames = ['occupancy','admissions','edvisits','ICU']
HospitalOccupancyByDay = {}
for day in range(0, endTime + 1):
hoc = []
for hsn in hospstatsnames:
for h in range(0, len(HospitalNames)):
hoc.append(0)
HospitalOccupancyByDay[day] = hoc
for i in range(0,len(RegionalList)):
if os.path.exists(ParameterSet.PopDataFolder + "/" + str(modelPopNames) + str(i) + "HOSPLIST.pickle"):
CurrentHospOccList = Utils.PickleFileRead(ParameterSet.PopDataFolder + "/" + str(modelPopNames) + str(i) + "HOSPLIST.pickle")
for key in CurrentHospOccList.keys():
tydict = CurrentHospOccList[key]
for key2 in tydict.keys():
hsdict = tydict[key2]
x = 0
for hsn in hospstatsnames:
lpdict = hsdict[hsn]
for h in range(0,len(lpdict)):
HospitalOccupancyByDay[key][x] += lpdict[h]
x += 1
csvFile = writefolder+"/HospitalOccupancyByDay_"+model+"_"+resultsName+".csv"
try:
with open(csvFile, 'w') as f:
f.write("day")
for hsn in hospstatsnames:
for h1 in range(0, len(HospitalNames)):
f.write(",%s_%s" % (HospitalNames[h1],hsn))
f.write("\n")
for key in HospitalOccupancyByDay.keys():
f.write("%s" % key)
for h in range(0, len(HospitalOccupancyByDay[key])):
f.write(",%s" % HospitalOccupancyByDay[key][h])
f.write("\n")
except IOError:
print("I/O error")
############
csvFileR0 = writefolder+"/R0_"+model+"_"+resultsName+".csv"
R0Stats = [0]*101
for i in range(0,len(RegionalList)):
if os.path.exists(ParameterSet.PopDataFolder + "/" + str(modelPopNames) + str(i) + "R0Stats.pickle"):
R0StatsList = Utils.PickleFileRead(ParameterSet.PopDataFolder + "/" + str(modelPopNames) + str(i) + "R0Stats.pickle")
for key in R0StatsList.keys():
R0Stat = R0StatsList[key]
for rkey in R0Stat.keys():
rvals = R0Stat[rkey]
for r in range(0,len(rvals)):
R0Stats[r] += rvals[r]
rnum = 0
rdenom = 0
for i in range(0,len(R0Stats)):
rdenom += R0Stats[i]
rnum += R0Stats[i]*i
np.savetxt(csvFileR0,R0Stats,delimiter=",", fmt='%5s')