-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCov14dayModel.py
More file actions
380 lines (299 loc) · 11.4 KB
/
Cov14dayModel.py
File metadata and controls
380 lines (299 loc) · 11.4 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import csv
from urllib.request import urlopen as uReq
import numpy as np
import scipy.stats
#import geopandas
import matplotlib.pyplot as plt
import sys
import serial
from serial.tools import list_ports
import time
UseTrueRNG = True
HALO = True
NEDspeed = 250#max = 50000
TurboSpeed = 250# max = 400000
CoThres = 0.5
NodeThres = 0.3141
outfile_path = 'C:/Users/Aslan/HALO'
starttime = time.time()
outfile = open('%s/CovModel_%d.txt'%(outfile_path,starttime),'w')
modfile = open('%s/CovModel_modfile_%d.txt'%(outfile_path,starttime),'w')
if UseTrueRNG==True:
ports=dict()
ports_avaiable = list(list_ports.comports())
rngcomports = []
turbocom = None
for temp in ports_avaiable:
if HALO==True:
if temp[1].startswith("TrueRNG"):
if 'pro' in temp[1]:
print ('found pro')
turbocom = str(temp[0])
else:
print('Found: ' + str(temp))
rngcomports.append(str(temp[0]))
else:
if temp[1].startswith("TrueRNG"):
print ('found device')
turbocom = str(temp[0])
if HALO==True:
ser = []
for a in range(0,len(rngcomports)):
ser.append (serial.Serial(port=rngcomports[a],timeout=10))
turboser= (serial.Serial(port=turbocom,timeout=10))
#print('Using com port: ' + str(rng1_com_port))
#print('Using com port: ' + str(rng2_com_port))
#print('==================================================')
sys.stdout.flush()
if HALO==True:
for a in range(0,len(rngcomports)):
if(ser[a].isOpen() == False):
ser[a].open()
ser[a].setDTR(True)
ser[a].flushInput()
if turboser.isOpen()==False:
turboser.open()
turboser.setDTR(True)
turboser.flushInput()
sys.stdout.flush()
def CohSampMain(params,Zthres,minruns):
TotalRuns=0
Zval = 0
bitct=[]
pct = []
allnodes=[]
for a in range (0,params):
pct.append([])
bads = np.zeros(params)
while np.abs(Zval)<Zthres or TotalRuns<minruns:
turboser.flushInput()
supernode = turboser.read(TurboSpeed)
for b in range (0,len(supernode)):
outfile.write('%d,'%(supernode[b]))
allnodes.append(supernode[b])
strnode = str(bin(256+int(supernode[b])))[3:]
bitct.append(int(strnode[0])+int(strnode[1])+int(strnode[2])+int(strnode[3])+int(strnode[4])+int(strnode[5])+int(strnode[6])+int(strnode[7]))
outfile.write('%d,T\n'%(int(time.time()*1000)))
for a in range(0,params):
if HALO==True:
ser[a].flushInput()
node = ser[a].read(NEDspeed)
else:
node = turboser.read(NEDspeed)
#print (a,len(node),TotalRuns)
if len(node)==0:
print('BAD READ ON %s'%rngcomports[a])
bads[a] += 1
else:
for mm in range (0,NEDspeed):
outfile.write('%d,'%(node[mm]))
strnum = bin(256+node[mm])[3:]
pct[a].append((int(strnum[0]) + int(strnum[1]) + int(strnum[2]) + int(strnum[3]) + int(strnum[4]) + int(strnum[5]) + int(strnum[6]) + int(strnum[7])))
outfile.write('%d,%d\n'%(int(time.time()*1000),a))
if TotalRuns < 300:
NedVal = np.sum(bitct)
TotalRuns += 1
#print(bitct)
else:
TotalRuns = 300
NedVal = np.sum(bitct[-(300*TurboSpeed):])
#print(bitct[-60:])
EX = NedVal-(TotalRuns*TurboSpeed*8*0.5)
snpq = (TotalRuns*TurboSpeed*8*0.25)**0.5
#print(TotalRuns,NedVal,EX,snpq)
Zval = EX/snpq
#print(Zval)
Z=[]
N = TotalRuns*NEDspeed*8
for a in range (0,params):
if TotalRuns < 300:
NedVal_x = np.sum(pct[a])
else:
NedVal_x = np.sum(pct[a][-(300*NEDspeed):])
Z.append((NedVal_x - (N*0.5)) / ((N*0.25)**0.5))
time.sleep(0.2)
#print(Z)
#print(pct)
#print(N)
return Z,allnodes[(-minruns*TurboSpeed):]
#red = geopandas.datasets.get_path('c_10nv20')
#world = geopandas.read_file(red)
#rworld = world.iloc
FIPS = []
LAT = []
LON = []
readFile = open('%s/FIPSout.txt'%outfile_path,'r')
sepfile = readFile.read().split('\n')
for a in range (0,len(sepfile)):
xandy = sepfile[a].split(',')
FIPS.append(int(xandy[0]))
LAT.append(float(xandy[1]))
LON.append(float(xandy[2]))
"""
for a in range (0,3323):
FIPS.append(int(rworld[a][3]))
LAT.append(float(rworld[a][7]))
LON.append(float(rworld[a][6]))
outfile = open('K:/FIPSout.txt','w')
for a in range (0,len(FIPS)):
outfile.write('%d,%f,%f\n'%(FIPS[a],LAT[a],LON[a]))
outfile.close()
"""
#counties = world.split('\n')
#print(len(rworld))
#print((rworld[3323]))
#geopandas.read_file('K:/counties/c_10nv20')
CoPop=[]
readFile = open('%s/CoPop.txt'%outfile_path,'r')
sepfile = readFile.read().split('\n')
for a in range (0,len(sepfile)):
CoPop.append(int(sepfile[a]))
COVdat = 'http://usafactsstatic.blob.core.windows.net/public/data/covid-19/covid_confirmed_usafacts.csv'
uClient = uReq(COVdat)
page_csv = str(uClient.read())
uClient.close()
sepfile = page_csv.split('\\r\\n')
ult_Q=[]
ult_pop=[]
ult_lat=[]
ult_lon=[]
ult_FIPS=[]
for a in range (2,len(sepfile)):
if CoPop[a-2]>0:
xandy = sepfile[a].split(',')
cfips = int(xandy[0])
ct=0
for b in range (0,len(FIPS)):
if FIPS[b]==cfips and ct==0:
ult_lat.append(LAT[b])
ult_lon.append(LON[b])
ult_FIPS.append(FIPS[b])
ct += 1
if ct==1:
xc = xandy[4:]
if '"' in xc[-1]:
d = int(xc[-1][:-1])
else:
d = int(xc[-1])
m14 = int(xc[-14])
ult_Q.append((d-m14)/CoPop[a-2])
ult_pop.append(CoPop[a-2])
print(len(ult_Q),len(ult_lat))
MeanQ = np.nanmean(ult_Q)
stdQ = np.nanstd(ult_Q)
ult_QZ = (np.array(ult_Q)-MeanQ)/stdQ
ult_pop_log = np.log10(np.array(ult_pop))
RankQ = scipy.stats.rankdata(ult_Q)
"""
Sig=[]
for a in range (0,len(ult_Q)):
sx = np.random.randint(0,2,1000)
Sig.append((np.sum(sx)-500)/((1000*0.25)**0.5))
RankS = scipy.stats.rankdata(Sig)
"""
def Cdist(lat1,lon1,lat2,lon2):
yy = (lat2-lat1)*111
xx = (lon2-lon1)*np.cos(((lat1+lat2)/2)*(np.pi/180))*111
dist = (((xx**2)+(yy**2))**0.5)
return dist
ult_pert=[]
OnlyNodesLat=[]
OnlyNodesLon=[]
OnlyNodesPert=[]
NodeCt = 0
for a in range (0,len(ult_pop)):
sel = CohSampMain(1,CoThres,50)[1]
nnode = ((sel[-3]%16)*(16**4)) + (sel[-2]*256) + (sel[-1])
#nnode = np.random.randint(0,2**20)
if nnode <= ult_pop[a]:#if number generated exceeds probabilistic threshold, we make the county a node
if len(OnlyNodesLat)>0:#
nDist=[]
for b in range (0,len(OnlyNodesLat)):
nDist.append(Cdist(OnlyNodesLat[b],OnlyNodesLon[b],ult_lat[a],ult_lon[a]))
if np.nanmin(nDist) > 100:#can only be a node if no previous nodes within 50 km
#sx = np.random.randint(0,2,1000)
#Pur = (np.sum(sx)-500)/((1000*0.25)**0.5)
print('working on node %d of about 130'%NodeCt)
sx = CohSampMain(1,NodeThres,300)
print('node complete')
Pur = sx[0][0]
sxx = sx[1]
for c in range (0,len(sxx)):
modfile.write('%d,'%(sxx[c]))
modfile.write('%d,%d,%f\n'%(time.time()*1000,ult_FIPS[a],Pur))
ult_pert.append(Pur)
NodeCt += 1
OnlyNodesLat.append(ult_lat[a])
OnlyNodesLon.append(ult_lon[a])
OnlyNodesPert.append(Pur)
else:
ult_pert.append(-9999)
else:
#sx = np.random.randint(0,2,1000)
#Pur = (np.sum(sx)-500)/((1000*0.25)**0.5)
print('working on node %d of about 130'%NodeCt)
sx = CohSampMain(1,NodeThres,300)
print('node complete')
Pur = sx[0][0]
sxx = sx[1]
for c in range (0,len(sxx)):
modfile.write('%d,'%(sxx[c]))
modfile.write('%d,%d,%f\n'%(time.time()*1000,ult_FIPS[a],Pur))
ult_pert.append(Pur)
NodeCt += 1
OnlyNodesLat.append(ult_lat[a])
OnlyNodesLon.append(ult_lon[a])
OnlyNodesPert.append(Pur)
else:
ult_pert.append(-9999)
if a%10==0:
print('county %d of %d complete'%(a,len(ult_pop)))
print(NodeCt)
ip_pert=[]
Rsave=[]
for a in range (0,len(ult_pert)):
if ult_pert[a]==-9999:
ff_weights=[]; ff_sig=[]
for b in range (0,len(ult_pert)):
if ult_pert[b]>-9999:
r2 = (Cdist(ult_lat[a],ult_lon[a],ult_lat[b],ult_lon[b])**2)
Rsave.append(r2)
ff_weights.append(1/r2)
ff_sig.append(ult_pert[b])
ip_pert.append(np.average(ff_sig,weights=ff_weights))
else:
ip_pert.append(ult_pert[a])
RankS = scipy.stats.rankdata(ip_pert)
co_R=[]
co_S=[]
for a in range (0,len(RankS)):
if ult_pert[a]>-9999:
#co_R.append(RankQ[a])
#co_S.append(RankS[a])
co_R.append(ult_QZ[a])
co_S.append(ip_pert[a])
fig = plt.figure()
ax1 = fig.add_subplot(131)
ax2 = fig.add_subplot(132)
ax3 = fig.add_subplot(133)
#ax1.scatter(ult_lon,ult_lat,c=RankS,cmap='seismic')
ax1.scatter(ult_lon,ult_lat,c=ip_pert,cmap='seismic',vmin=-2,vmax=2)
ax1.scatter(OnlyNodesLon,OnlyNodesLat,c=OnlyNodesPert,marker = '^',cmap = 'seismic',vmin=-2,vmax=2)#what we're showing is RANK so the flip isn't quite clear around some points. Needs to be rank? Further its rank Q and not rank QZ
#ax2.scatter(ult_lon,ult_lat,c=RankQ,cmap='seismic')
ax2.scatter(ult_lon,ult_lat,c=ult_QZ,cmap='seismic',vmin=-2,vmax=2)
ax3.scatter(co_R,co_S)
plt.show()
MidS = np.median(co_S)
MidQ = np.median(co_R)
hit = 0
for a in range (0,len(co_S)):
if (co_S[a]>MidS and co_R[a]>MidQ) or (co_S[a]<MidS and co_R[a]<MidQ):
hit += 1
nZ = len(co_S)
Z = (hit-(nZ*0.5))/((nZ*0.25)**0.5)
print('hits: %d, total: %d, Z: %f'%(hit,nZ,Z))
#do NOT want to weight this score by population, nodes in rural areas are important as they speak to same population broader area. Also this is a case against variable A0 based on node population. Just ML hits and misses. And where note to do this for WRF as well? (can do RETROACTIVELY and see if works. As can with LO with meaning applied. etc. document this.)
#correlate only nodes. A0 based on log(pop).
#visual check binomal hit/miss with this. A0 + previous data learn and look for that as new point.(after 2w)...so ML model applied after first . A0~pop
outfile.close()
modfile.close()